How to parse a date? [duplicate]
This question already has an answer here:
Java string to date conversion
13 answers
I am trying to parse this date with SimpleDateFormat
and it is not working:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
If I try this code with strDate="2008-10-14"
, I have a positive answer. What's the problem? How can I parse this format?
PS. I got this date from a jDatePicker
and there is no instruction on how modify the date format I get when the user chooses a date.
java date simpledateformat
marked as duplicate by BalusC
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Java string to date conversion
13 answers
I am trying to parse this date with SimpleDateFormat
and it is not working:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
If I try this code with strDate="2008-10-14"
, I have a positive answer. What's the problem? How can I parse this format?
PS. I got this date from a jDatePicker
and there is no instruction on how modify the date format I get when the user chooses a date.
java date simpledateformat
marked as duplicate by BalusC
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02
add a comment |
This question already has an answer here:
Java string to date conversion
13 answers
I am trying to parse this date with SimpleDateFormat
and it is not working:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
If I try this code with strDate="2008-10-14"
, I have a positive answer. What's the problem? How can I parse this format?
PS. I got this date from a jDatePicker
and there is no instruction on how modify the date format I get when the user chooses a date.
java date simpledateformat
This question already has an answer here:
Java string to date conversion
13 answers
I am trying to parse this date with SimpleDateFormat
and it is not working:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Formaterclass {
public static void main(String args) throws ParseException{
String strDate = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("yyyy-MM-dd date is ==>"+formattedDate);
Date date1 = formatter.parse(formattedDate);
formatter = new SimpleDateFormat("dd-MMM-yyyy");
formattedDate = formatter.format(date1);
System.out.println("dd-MMM-yyyy date is ==>"+formattedDate);
}
}
If I try this code with strDate="2008-10-14"
, I have a positive answer. What's the problem? How can I parse this format?
PS. I got this date from a jDatePicker
and there is no instruction on how modify the date format I get when the user chooses a date.
This question already has an answer here:
Java string to date conversion
13 answers
java date simpledateformat
java date simpledateformat
edited Jul 1 '13 at 17:12
Nathaniel Ford
13.6k145473
13.6k145473
asked Jun 16 '09 at 1:01
fenecfenec
2,37784275
2,37784275
marked as duplicate by BalusC
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by BalusC
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 17 '16 at 21:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
4
For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02
add a comment |
4
For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02
4
4
For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02
For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02
add a comment |
5 Answers
5
active
oldest
votes
You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):
SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
String input = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
...
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
11
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
@jwl how to parceSun Dec 06 11:15:00 IST 2015
this date
– Sagar
Dec 11 '18 at 9:21
What ifLocale
changes not able parse on4.4 and 5.1
devices
– Sagar
Dec 12 '18 at 5:51
Note thatSimpleDateFormat
is configured with aTimeZone
either by default or by calling thesetTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.
– Marcus Junius Brutus
Jan 8 at 21:08
add a comment |
The problem is that you have a date formatted like this:
Thu Jun 18 20:56:02 EDT 2009
But are using a SimpleDateFormat
that is:
yyyy-MM-dd
The two formats don't agree. You need to construct a SimpleDateFormat
that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat
like this:
EEE MMM dd HH:mm:ss zzz yyyy
Thu Jun 18 20:56:02 EDT 2009
Check the JavaDoc page I linked to and see how the characters are used.
1
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
@eddie how to parceSun Dec 06 11:15:00 IST 2015
date todd mm yy
– Sagar
Dec 11 '18 at 9:26
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
add a comment |
We now have a more modern way to do this work.
java.time
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Note that the 3-4 letter codes like EDT
are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.
By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.
String input = "Thu Jun 18 20:56:02 EDT 2009";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );
Dump to console.
System.out.println ( "zdt : " + zdt );
When run.
zdt : 2009-06-18T20:56:02-04:00[America/New_York]
Adjust Time Zone
For fun let's adjust to the India time zone.
ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );
zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]
Convert to j.u.Date
If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.
java.util.Date date = java.util.Date.from( zdt.toInstant() );
how to parceSun Dec 06 11:15:00 IST 2015
date format todd mm yyyy
i am not able to parce withSimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with botZ
andz
– Sagar
Dec 11 '18 at 9:27
@SagarHudge UseDateTimeFormatter
notSimpleDateFormat
. Reread my Answer.
– Basil Bourque
Dec 11 '18 at 16:14
The device targeting4.4 eureka
andAsus 5.1
having parsing issue on app language change to non English languages likeMarathi/Hindi
normal date able to parse ofdd mm yyyy
– Sagar
Dec 12 '18 at 5:17
@SagarHudge Specify aLocale
on theDateTimeFormatter
to determine the human language and cultural norms used in translation.
– Basil Bourque
Dec 12 '18 at 6:12
DateTimeFormatter
targets26
having issue on4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
|
show 1 more comment
How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:
new SimpleDateFormat("yyyy-MM-dd");
The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"
add a comment |
In response to:
"How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate
Try this:
With java.util.Date
, java.text.SimpleDateFormat
, it's a simple solution.
public static void main(String args) throws ParseException {
String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
Date f = new Date(fecha);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
fecha = sdf.format(f);
System.out.println(fecha);
}
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):
SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
String input = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
...
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
11
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
@jwl how to parceSun Dec 06 11:15:00 IST 2015
this date
– Sagar
Dec 11 '18 at 9:21
What ifLocale
changes not able parse on4.4 and 5.1
devices
– Sagar
Dec 12 '18 at 5:51
Note thatSimpleDateFormat
is configured with aTimeZone
either by default or by calling thesetTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.
– Marcus Junius Brutus
Jan 8 at 21:08
add a comment |
You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):
SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
String input = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
...
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
11
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
@jwl how to parceSun Dec 06 11:15:00 IST 2015
this date
– Sagar
Dec 11 '18 at 9:21
What ifLocale
changes not able parse on4.4 and 5.1
devices
– Sagar
Dec 12 '18 at 5:51
Note thatSimpleDateFormat
is configured with aTimeZone
either by default or by calling thesetTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.
– Marcus Junius Brutus
Jan 8 at 21:08
add a comment |
You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):
SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
String input = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
...
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You cannot expect to parse a date with a SimpleDateFormat that is set up with a different format.
To parse your "Thu Jun 18 20:56:02 EDT 2009" date string you need a SimpleDateFormat like this (roughly):
SimpleDateFormat parser=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Use this to parse the string into a Date, and then your other SimpleDateFormat to turn that Date into the format you want.
String input = "Thu Jun 18 20:56:02 EDT 2009";
SimpleDateFormat parser = new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");
Date date = parser.parse(input);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(date);
...
JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
edited Sep 30 '16 at 21:46
answered Jun 16 '09 at 1:09
jwljwl
6,530123980
6,530123980
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
11
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
@jwl how to parceSun Dec 06 11:15:00 IST 2015
this date
– Sagar
Dec 11 '18 at 9:21
What ifLocale
changes not able parse on4.4 and 5.1
devices
– Sagar
Dec 12 '18 at 5:51
Note thatSimpleDateFormat
is configured with aTimeZone
either by default or by calling thesetTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.
– Marcus Junius Brutus
Jan 8 at 21:08
add a comment |
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
11
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
@jwl how to parceSun Dec 06 11:15:00 IST 2015
this date
– Sagar
Dec 11 '18 at 9:21
What ifLocale
changes not able parse on4.4 and 5.1
devices
– Sagar
Dec 12 '18 at 5:51
Note thatSimpleDateFormat
is configured with aTimeZone
either by default or by calling thesetTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.
– Marcus Junius Brutus
Jan 8 at 21:08
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
having the same issue ... it works good on Java7, but fails on parsing "Thu Jun ..." as "EEE MMM ..." in Java6. Any ideas ?
– d.raev
Apr 1 '13 at 11:47
11
11
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
eventuell new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy",Locale.ENGLISH); could be needed if current locale != ENGLISH
– Nightingale7
Apr 22 '13 at 13:44
@jwl how to parce
Sun Dec 06 11:15:00 IST 2015
this date– Sagar
Dec 11 '18 at 9:21
@jwl how to parce
Sun Dec 06 11:15:00 IST 2015
this date– Sagar
Dec 11 '18 at 9:21
What if
Locale
changes not able parse on 4.4 and 5.1
devices– Sagar
Dec 12 '18 at 5:51
What if
Locale
changes not able parse on 4.4 and 5.1
devices– Sagar
Dec 12 '18 at 5:51
Note that
SimpleDateFormat
is configured with a TimeZone
either by default or by calling the setTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.– Marcus Junius Brutus
Jan 8 at 21:08
Note that
SimpleDateFormat
is configured with a TimeZone
either by default or by calling the setTimeZone
method. That time zone is used during parsing if and only if a time zone is not indicated in the string to be parsed.– Marcus Junius Brutus
Jan 8 at 21:08
add a comment |
The problem is that you have a date formatted like this:
Thu Jun 18 20:56:02 EDT 2009
But are using a SimpleDateFormat
that is:
yyyy-MM-dd
The two formats don't agree. You need to construct a SimpleDateFormat
that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat
like this:
EEE MMM dd HH:mm:ss zzz yyyy
Thu Jun 18 20:56:02 EDT 2009
Check the JavaDoc page I linked to and see how the characters are used.
1
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
@eddie how to parceSun Dec 06 11:15:00 IST 2015
date todd mm yy
– Sagar
Dec 11 '18 at 9:26
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
add a comment |
The problem is that you have a date formatted like this:
Thu Jun 18 20:56:02 EDT 2009
But are using a SimpleDateFormat
that is:
yyyy-MM-dd
The two formats don't agree. You need to construct a SimpleDateFormat
that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat
like this:
EEE MMM dd HH:mm:ss zzz yyyy
Thu Jun 18 20:56:02 EDT 2009
Check the JavaDoc page I linked to and see how the characters are used.
1
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
@eddie how to parceSun Dec 06 11:15:00 IST 2015
date todd mm yy
– Sagar
Dec 11 '18 at 9:26
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
add a comment |
The problem is that you have a date formatted like this:
Thu Jun 18 20:56:02 EDT 2009
But are using a SimpleDateFormat
that is:
yyyy-MM-dd
The two formats don't agree. You need to construct a SimpleDateFormat
that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat
like this:
EEE MMM dd HH:mm:ss zzz yyyy
Thu Jun 18 20:56:02 EDT 2009
Check the JavaDoc page I linked to and see how the characters are used.
The problem is that you have a date formatted like this:
Thu Jun 18 20:56:02 EDT 2009
But are using a SimpleDateFormat
that is:
yyyy-MM-dd
The two formats don't agree. You need to construct a SimpleDateFormat
that matches the layout of the string you're trying to parse into a Date. Lining things up to make it easy to see, you want a SimpleDateFormat
like this:
EEE MMM dd HH:mm:ss zzz yyyy
Thu Jun 18 20:56:02 EDT 2009
Check the JavaDoc page I linked to and see how the characters are used.
edited Dec 19 '15 at 9:50
Abdelhak
7,71431331
7,71431331
answered Jun 16 '09 at 1:08
EddieEddie
45.4k18109136
45.4k18109136
1
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
@eddie how to parceSun Dec 06 11:15:00 IST 2015
date todd mm yy
– Sagar
Dec 11 '18 at 9:26
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
add a comment |
1
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
@eddie how to parceSun Dec 06 11:15:00 IST 2015
date todd mm yy
– Sagar
Dec 11 '18 at 9:26
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
1
1
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
I really wanted to up-vote this but you have exactly 42 up-votes, sorry!
– Madbreaks
Jun 6 '16 at 17:56
@eddie how to parce
Sun Dec 06 11:15:00 IST 2015
date to dd mm yy
– Sagar
Dec 11 '18 at 9:26
@eddie how to parce
Sun Dec 06 11:15:00 IST 2015
date to dd mm yy
– Sagar
Dec 11 '18 at 9:26
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
What if Locale changes not able parse on 4.4 and 5.1 devices
– Sagar
Dec 12 '18 at 5:52
add a comment |
We now have a more modern way to do this work.
java.time
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Note that the 3-4 letter codes like EDT
are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.
By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.
String input = "Thu Jun 18 20:56:02 EDT 2009";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );
Dump to console.
System.out.println ( "zdt : " + zdt );
When run.
zdt : 2009-06-18T20:56:02-04:00[America/New_York]
Adjust Time Zone
For fun let's adjust to the India time zone.
ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );
zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]
Convert to j.u.Date
If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.
java.util.Date date = java.util.Date.from( zdt.toInstant() );
how to parceSun Dec 06 11:15:00 IST 2015
date format todd mm yyyy
i am not able to parce withSimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with botZ
andz
– Sagar
Dec 11 '18 at 9:27
@SagarHudge UseDateTimeFormatter
notSimpleDateFormat
. Reread my Answer.
– Basil Bourque
Dec 11 '18 at 16:14
The device targeting4.4 eureka
andAsus 5.1
having parsing issue on app language change to non English languages likeMarathi/Hindi
normal date able to parse ofdd mm yyyy
– Sagar
Dec 12 '18 at 5:17
@SagarHudge Specify aLocale
on theDateTimeFormatter
to determine the human language and cultural norms used in translation.
– Basil Bourque
Dec 12 '18 at 6:12
DateTimeFormatter
targets26
having issue on4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
|
show 1 more comment
We now have a more modern way to do this work.
java.time
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Note that the 3-4 letter codes like EDT
are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.
By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.
String input = "Thu Jun 18 20:56:02 EDT 2009";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );
Dump to console.
System.out.println ( "zdt : " + zdt );
When run.
zdt : 2009-06-18T20:56:02-04:00[America/New_York]
Adjust Time Zone
For fun let's adjust to the India time zone.
ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );
zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]
Convert to j.u.Date
If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.
java.util.Date date = java.util.Date.from( zdt.toInstant() );
how to parceSun Dec 06 11:15:00 IST 2015
date format todd mm yyyy
i am not able to parce withSimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with botZ
andz
– Sagar
Dec 11 '18 at 9:27
@SagarHudge UseDateTimeFormatter
notSimpleDateFormat
. Reread my Answer.
– Basil Bourque
Dec 11 '18 at 16:14
The device targeting4.4 eureka
andAsus 5.1
having parsing issue on app language change to non English languages likeMarathi/Hindi
normal date able to parse ofdd mm yyyy
– Sagar
Dec 12 '18 at 5:17
@SagarHudge Specify aLocale
on theDateTimeFormatter
to determine the human language and cultural norms used in translation.
– Basil Bourque
Dec 12 '18 at 6:12
DateTimeFormatter
targets26
having issue on4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
|
show 1 more comment
We now have a more modern way to do this work.
java.time
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Note that the 3-4 letter codes like EDT
are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.
By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.
String input = "Thu Jun 18 20:56:02 EDT 2009";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );
Dump to console.
System.out.println ( "zdt : " + zdt );
When run.
zdt : 2009-06-18T20:56:02-04:00[America/New_York]
Adjust Time Zone
For fun let's adjust to the India time zone.
ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );
zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]
Convert to j.u.Date
If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.
java.util.Date date = java.util.Date.from( zdt.toInstant() );
We now have a more modern way to do this work.
java.time
The java.time framework is bundled with Java 8 and later. See Tutorial. These new classes are inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen-Extra project. They are a vast improvement over the troublesome old classes, java.util.Date/.Calendar et al.
Note that the 3-4 letter codes like EDT
are neither standardized nor unique. Avoid them whenever possible. Learn to use ISO 8601 standard formats instead. The java.time framework may take a stab at translating, but many of the commonly used codes have duplicate values.
By the way, note how java.time by default generates strings using the ISO 8601 formats but extended by appending the name of the time zone in brackets.
String input = "Thu Jun 18 20:56:02 EDT 2009";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern ( "EEE MMM d HH:mm:ss zzz yyyy" , Locale.ENGLISH );
ZonedDateTime zdt = formatter.parse ( input , ZonedDateTime :: from );
Dump to console.
System.out.println ( "zdt : " + zdt );
When run.
zdt : 2009-06-18T20:56:02-04:00[America/New_York]
Adjust Time Zone
For fun let's adjust to the India time zone.
ZonedDateTime zdtKolkata = zdt.withZoneSameInstant ( ZoneId.of ( "Asia/Kolkata" ) );
zdtKolkata : 2009-06-19T06:26:02+05:30[Asia/Kolkata]
Convert to j.u.Date
If you really need a java.util.Date object for use with classes not yet updated to the java.time types, convert. Note that you are losing the assigned time zone, but have the same moment automatically adjusted to UTC.
java.util.Date date = java.util.Date.from( zdt.toInstant() );
edited Oct 11 '15 at 1:52
answered Oct 11 '15 at 1:08
Basil BourqueBasil Bourque
109k26375538
109k26375538
how to parceSun Dec 06 11:15:00 IST 2015
date format todd mm yyyy
i am not able to parce withSimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with botZ
andz
– Sagar
Dec 11 '18 at 9:27
@SagarHudge UseDateTimeFormatter
notSimpleDateFormat
. Reread my Answer.
– Basil Bourque
Dec 11 '18 at 16:14
The device targeting4.4 eureka
andAsus 5.1
having parsing issue on app language change to non English languages likeMarathi/Hindi
normal date able to parse ofdd mm yyyy
– Sagar
Dec 12 '18 at 5:17
@SagarHudge Specify aLocale
on theDateTimeFormatter
to determine the human language and cultural norms used in translation.
– Basil Bourque
Dec 12 '18 at 6:12
DateTimeFormatter
targets26
having issue on4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
|
show 1 more comment
how to parceSun Dec 06 11:15:00 IST 2015
date format todd mm yyyy
i am not able to parce withSimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with botZ
andz
– Sagar
Dec 11 '18 at 9:27
@SagarHudge UseDateTimeFormatter
notSimpleDateFormat
. Reread my Answer.
– Basil Bourque
Dec 11 '18 at 16:14
The device targeting4.4 eureka
andAsus 5.1
having parsing issue on app language change to non English languages likeMarathi/Hindi
normal date able to parse ofdd mm yyyy
– Sagar
Dec 12 '18 at 5:17
@SagarHudge Specify aLocale
on theDateTimeFormatter
to determine the human language and cultural norms used in translation.
– Basil Bourque
Dec 12 '18 at 6:12
DateTimeFormatter
targets26
having issue on4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
how to parce
Sun Dec 06 11:15:00 IST 2015
date format to dd mm yyyy
i am not able to parce with SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with bot Z
and z
– Sagar
Dec 11 '18 at 9:27
how to parce
Sun Dec 06 11:15:00 IST 2015
date format to dd mm yyyy
i am not able to parce with SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH)
tried with bot Z
and z
– Sagar
Dec 11 '18 at 9:27
@SagarHudge Use
DateTimeFormatter
not SimpleDateFormat
. Reread my Answer.– Basil Bourque
Dec 11 '18 at 16:14
@SagarHudge Use
DateTimeFormatter
not SimpleDateFormat
. Reread my Answer.– Basil Bourque
Dec 11 '18 at 16:14
The device targeting
4.4 eureka
and Asus 5.1
having parsing issue on app language change to non English languages like Marathi/Hindi
normal date able to parse of dd mm yyyy
– Sagar
Dec 12 '18 at 5:17
The device targeting
4.4 eureka
and Asus 5.1
having parsing issue on app language change to non English languages like Marathi/Hindi
normal date able to parse of dd mm yyyy
– Sagar
Dec 12 '18 at 5:17
@SagarHudge Specify a
Locale
on the DateTimeFormatter
to determine the human language and cultural norms used in translation.– Basil Bourque
Dec 12 '18 at 6:12
@SagarHudge Specify a
Locale
on the DateTimeFormatter
to determine the human language and cultural norms used in translation.– Basil Bourque
Dec 12 '18 at 6:12
DateTimeFormatter
targets 26
having issue on 4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
DateTimeFormatter
targets 26
having issue on 4.4 anf 5.1 device
– Sagar
Dec 12 '18 at 6:17
|
show 1 more comment
How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:
new SimpleDateFormat("yyyy-MM-dd");
The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"
add a comment |
How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:
new SimpleDateFormat("yyyy-MM-dd");
The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"
add a comment |
How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:
new SimpleDateFormat("yyyy-MM-dd");
The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"
How about getSelectedDate? Anyway, specifically on your code question, the problem is with this line:
new SimpleDateFormat("yyyy-MM-dd");
The string that goes in the constructor has to match the format of the date. The documentation for how to do that is here. Looks like you need something close to "EEE MMM d HH:mm:ss zzz yyyy"
answered Jun 16 '09 at 1:10
YishaiYishai
72k20159237
72k20159237
add a comment |
add a comment |
In response to:
"How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate
Try this:
With java.util.Date
, java.text.SimpleDateFormat
, it's a simple solution.
public static void main(String args) throws ParseException {
String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
Date f = new Date(fecha);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
fecha = sdf.format(f);
System.out.println(fecha);
}
add a comment |
In response to:
"How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate
Try this:
With java.util.Date
, java.text.SimpleDateFormat
, it's a simple solution.
public static void main(String args) throws ParseException {
String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
Date f = new Date(fecha);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
fecha = sdf.format(f);
System.out.println(fecha);
}
add a comment |
In response to:
"How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate
Try this:
With java.util.Date
, java.text.SimpleDateFormat
, it's a simple solution.
public static void main(String args) throws ParseException {
String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
Date f = new Date(fecha);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
fecha = sdf.format(f);
System.out.println(fecha);
}
In response to:
"How to convert Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México)) to dd-MM-yy in Java?", it was marked how duplicate
Try this:
With java.util.Date
, java.text.SimpleDateFormat
, it's a simple solution.
public static void main(String args) throws ParseException {
String fecha = "Tue Sep 13 2016 00:00:00 GMT-0500 (Hora de verano central (México))";
Date f = new Date(fecha);
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setTimeZone(TimeZone.getTimeZone("-5GMT"));
fecha = sdf.format(f);
System.out.println(fecha);
}
edited Sep 6 '16 at 5:45
answered Sep 6 '16 at 5:29
Edgar CivilEdgar Civil
114
114
add a comment |
add a comment |
4
For parsing/formatting dates with Java 8, have a look at this answer: stackoverflow.com/questions/22463062/…
– micha
Mar 30 '14 at 10:02