Error by converting date: Unparseable date in JAVA - java

I can't find the problem. I'm trying to convert the date:
"Thu, 10 Jul 2014 13:33:26 +0200"
from string to Date with this code:
String formatType = "EEE, dd MMM yyyy HH:mm:ss Z";
Date startzeit = new SimpleDateFormat(formatType).parse(einsatz.getString("startzeit"));
but I'm getting this exceptoin:
java.text.ParseException: Unparseable date: "Thu, 10 Jul 2014 13:33:26 +0200"

You're creating a SimpleDateFormat without specifying a locale, so it'll use the default locale. By the looks of your variable names, that may not be English - so it'll have a hard time parsing "Thu" and "Jul".
Try:
String formatType = "EEE, dd MMM yyyy HH:mm:ss Z";
Date startzeit = new SimpleDateFormat(formatType, Locale.US)
.parse(einsatz.getString("startzeit");
(That works for me, with your sample value.)

Related

Unparseable Date: 'Day name in week' (SimpleDateFormat) [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 5 years ago.
I'm trying to convert a date from String to a Date object:
String dateString = "Mon, 04 Sep 2017 18:30:28";
String dateFormat = "EEE, dd MMM yyyy HH:mm:ss";
Results in the following exception:
java.text.ParseException: Unparseable date: "Mon, 04 Sep 2017 18:30:28"
I tried different strings and formats and the problems seems to be the name of week ('EEE'). Without it works perfectly.
Also this works perfectly:
String dateString = "04 Sep 2017 18:30:28, Mon";
String dateFormat = "dd MMM yyyy HH:mm:ss, EEE";
Month and weekdays are abbreviated text. The text is language dependent and if you don't provide a specific Locale when instantiating the SimpleDateFormatter the system's Locale is used instead. The reason why your parsing fails with the weekday and not the month can be that the abbreviated name of the month in your system's default language is coincidentally the same as in English.
Here is some code how you should parse a date with text:
SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
formatter.setLenient(false);
ParsePosition pos = new ParsePosition(0);
return formatter.parse(toParse, pos);
with toParse being a string containing your date as text.
You haven't added the locale which must have caused parse exception while parsing text(s) for Day Of Week & Month
public static void main(String[] args) throws ParseException {
String dateString = "Mon, 04 Sep 2017 18:30:28";
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
System.out.println(dateFormat.parse(dateString));
}
Produces the following output
Mon Sep 04 18:30:28 IST 2017

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! :)

Getting incorrect parsed date when using SimpleDateTimeFormatter with time zone

I've the following code:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String s2 = "Mon Oct 19 19:52:21 IST 2015";
System.out.println(format.parse(s2));
I expected that the code would print "Mon Oct 19 07:22:21 PDT 2015", but to my surprise it printed "Mon Oct 19 10:52:21 PDT 2015". I can see that "IST" is a valid time zone for "z". However, the following code worked fine:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
String s2 = "Mon Oct 19 19:52:21 GMT+05:30 2015";
System.out.println(format.parse(s2));
Please help.
Using IST is dangerous as it could stand for Indian or Israel ST.
Assuming you meant Indian ST, try this:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
format.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
String s2 = "Mon Oct 19 19:52:21 IST 2015";
System.out.println(format.parse(s2));
Also, follow suggestion from #Meno's answer
The code expression
System.out.println(format.parse(s2));
always prints your instant/moment (an object of type java.util.Date) in your system timezone which is apparently PDT. You implicitly apply the method toString()on java.util.Date. Use a dedicated formatter like SimpleDateFormat and set the timezone on this formatter to achieve the desired result.
Finally you need two formatters, one for parsing and one for printing.

wrong date in SimpleDateTime parse

This is the string that I have:
Sat, Nov 02, 2013 at 5:10 pm
I'm trying to parse it into a date time using this formatter:
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, YYYY 'at' K:mm a");
However, this is what it returns when I use it to parse the date string:
Sat Jan 05 17:10:00 CST 2013
I assume I'm getting the formatter wrong, but I can't figure out where.
Capital YYYY is the format for something called the "week year". You want the lowercase yyyy for the actual year.
DateFormat formatter = new SimpleDateFormat("EEE, MMM dd, yyyy 'at' K:mm a");
With this change I output the parsed date and get:
Sat Nov 02 17:10:00 PDT 2013
(I'm in the Pacific time zone.)

Date format parse exception - "EEE MMM dd HH:mm:ss Z yyyy" [duplicate]

This question already has answers here:
How to convert date in to yyyy-MM-dd Format?
(6 answers)
How can I convert Date.toString back to Date?
(5 answers)
Java - Unparseable date
(3 answers)
Closed 5 years ago.
I got problem with date parse example date:
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.getDefault());
parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
got exception
Exacly I want parse this format date to yyyy-MM-dd
I try:
SimpleDateFormat parserSDF = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
take :
java.text.ParseException: Unparseable date: "Wed Oct 16 00:00:00 CEST 2013"
OK I change to and works :
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
I'm going to assume that Locale.getDefault() for you is pl-PL since you seem to be in Poland.
English words in date strings therefore cause an unparseable date.
An appropriate Polish date String would be something like
"Wt paź 16 00:00:00 -0500 2013"
Otherwise, change your Locale to Locale.ENGLISH so that the SimpleDateFormat object can parse String dates with English words.
Instead of using Locale.default that you and others often don't know which default, you can decide by using locale.ENGLISH because I see your string date is format in English. If you are at other countries, the format will be different.
Here is my example code:
public static void main(String[] args) {
try {
SimpleDateFormat parserSDF = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date date = parserSDF.parse("Wed Oct 16 00:00:00 CEST 2013");
System.out.println("date: " + date.toString());
} catch (ParseException ex) {
ex.printStackTrace();
}
}
The result will be : date: Wed Oct 16 05:00:00 ICT 2013. Or you can decide which part of this date to be printed, by using its fields.
Hope this help :)
I think the original Exception is due to Z in your format.
Per documentation:
Z Time zone RFC 822 time zone -0800
most likely you meant to use lower case z

Categories

Resources