Convert String to Date error [duplicate] - java

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 7 years ago.
I try to convert string to date format, but the following does't work.
String stringdate = "Fri Mar 27 17:14:27 EET 2015";
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
try {
Date newdate = format.parse(stringdate);
} catch (ParseException e) {
e.printStackTrace();
}
the output is
java.text.ParseException: Unparseable date: "Fri Mar 27 17:14:27 EET
2015"

SimpleDateFormat is locale-sensitive, so your default locale may be the reason why the exception is thrown. Try setting the locale to US.
DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

Related

Need help parsing String into date [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical
(4 answers)
Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]
(3 answers)
Closed 3 years ago.
So I get this String ("Tue Nov 26 12:05:19 CET 2019") from a txt fiel and I want to parse it into a Date like this:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date date = null;
try {
date = format.parse(dateAsString);
} catch (ParseException e) {
e.printStackTrace();
}
And I still get this Exception:
java.text.ParseException: Unparseable date: "Tue Nov 26 12:05:19 CET 2019"
But the format/patter should be ok. So my question is how I parse the string into a date.
You are most likely not using English locale so Tue and Nov are not parsing. Specify the locale with the formatter and don't use obsolete date classes:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
ZonedDateTime time = ZonedDateTime.parse("Tue Nov 26 12:05:19 CET 2019", fmt);
System.out.println(time); // 2019-11-26T12:05:19+01:00[Europe/Paris]
you can set language as English here
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Date date = null;
try {
date = format.parse("Tue Nov 26 12:05:19 CET 2019");
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(date);
This worked for me:
SimpleDateFormat format =
new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy", new Locale("en", "EN"));

java.text.ParseException - With right format [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
How to Convert RFC-1123 date-time formatter, to local time
(1 answer)
Closed 4 years ago.
I'm trying to parse this date
Thu, 15 Nov 2018 16:56:49 +0000
With this code:
Date date = null;
try {
SimpleDateFormat parser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
date = parser.parse(xmlPullParser.nextText());
} catch (ParseException e) {
e.printStackTrace();
date = new Date(); //This is just a temporary workaround
}
java.text.ParseException: Unparseable date: "Thu, 15 Nov 2018 16:56:49
+0000"
I've already try this formats too
EEE, dd MMM yyyy HH:mm:ss sssZ
EEE, d MMM yyyy HH:mm:ss sssZ
EEE, d MMM yyyy HH:mm:ss Z
But obviously it doesn't work
You forgot to set the Locale of SimpleDateFormat. Since you try to read a date in a english form, I would initialize this way:
SimpleDateFormat parser = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
When you don't specify any Locale, it uses the Locale of your system which is obviously not US or UK.

Java date does not parse day [duplicate]

This question already has answers here:
Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]
(3 answers)
format date from "MMM dd, yyyy HH:mm:ss a" to "MM.dd
(2 answers)
Closed 7 years ago.
I have a weird problem, when I try to parse this date: Tue Nov 03 10:50:16 CET 2015 using the java SimpleDateFormat, it throws an exception because of the "Tue" in there.
My code is:
String date = "Tue Nov 03 10:50:16 CET 2015";
Date parsedDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(date);
Which throws this exception: java.text.ParseException: Unparseable date: "Tue Nov 03 10:50:16 CET 2015"
I have tried debugging it, and this is what is boils down to:
String date = "Tue";
Date parsedDate = new SimpleDateFormat("EEE").parse(date);
Which throws the same type of exception. (I also tried it with a single 'E'). I think this is really strange, because the documentation tells me that this is how it should be used. Source: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
Solutions are more than welcome!
Thanks, Luca
Update: the point of the parsing is to parse MANIFEST.getMainAttributes().getValue("Created-On");
Thanks to cheffe I figured out the solution:
Date parsedDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(date);
Which worked! :)

How to parse the string "Mon Oct 22 03:00:26 +0000 2012" [duplicate]

This question already has answers here:
How to parse a date? [duplicate]
(5 answers)
Closed 9 years ago.
How to parse the following string to date
Mon Oct 22 03:00:26 +0000 2012
I tried MMM dd HH:mm:ss yyyy, but it is not working. I know I am missing something but couldn't find out that.
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
Date d=(Date)a.parse(b)
I'd recommend EEE MMM dd HH:mm:ss Z yyyy instead of HH:mm:ss yyyy.
Edit:
Specifically, your code would be:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy");
Date d=(Date)a.parse(b)
Edit after comment:
String b="Mon Oct 22 03:00:26 +0000 2012";
DateFormat a = new SimpleDateFormat("MMM dd HH:mm:ss Z yyyy", Locale.getDefault());
Date d=(Date)a.parse(b)
Try to do something like this:
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Ref: How to Convert String to Date in Java

Date format conversion error [duplicate]

This question already has answers here:
SimpleDateFormat ignoring month when parsing
(4 answers)
Closed 9 years ago.
I am facing the problem while converting the date:
Current format is:Thu Sep 05 12:07:46 IST 2013(dow mon dd hh:mm:ss zzz yyyy)
I need to convert in to:09/04/2013 11:38 PM PDT(mm/dd/yyyy hh:mm a zzz)
But i am not able to convert.
Try using SimpleDateFormatter. You have to tell it the input/output format, you can do that based on this description (you can also find a few common examples there).
The code will be something like this:
try {
String input = "Thu Sep 05 12:07:46 IST 2013";
DateFormat formatter = new SimpleDateFormat("I leave this to you :-)))");
System.out.println(formatter.parse(input));
} catch (ParseException e) {
e.printStackTrace();
}
Hope that helps.
You can do this
TimeZone tz = TimeZone.getTimeZone("PST8PDT"); // example
// required format. Remember M is for month, m for miniute
DateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm a zzz");
df.setTimeZone(tz);
String text = df.format(new Date());// current time
System.out.println(text);
Also please check this TimeZones in Java
You try to convert dateformat and timeZone as well, so you need to convert the timezone in your code.
SimpleDateFormat sf = new SimpleDateFormat("dow mon dd hh:mm:ss zzz yyyy");
isoFormat.setTimeZone(TimeZone.getTimeZone("PDT"));
Date date = isoFormat.parse("mm/dd/yyyy hh:mm a zzz");
this may help you.
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
OutPut
09/05/2013 00:07:46 AM IST
update
try {
DateFormat dffrom = new SimpleDateFormat("E MMM dd hh:mm:ss zzz yyyy");
DateFormat dfto = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a zzz");
TimeZone zone = TimeZone.getTimeZone("America/Los_Angeles");
dfto.setTimeZone(zone);
Date date = dffrom.parse("Thu Sep 05 12:07:46 IST 2013");
String s = dfto.format(date);
System.out.println(s);
} catch (ParseException e) {
}
output
09/04/2013 11:37:46 AM PDT

Categories

Resources