Java date does not parse day [duplicate] - java

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

Related

How to parse date string EEE MMM dd to yyyy-MM-dd in java [duplicate]

This question already has answers here:
Change date format in a Java string
(22 answers)
how to parse output of new Date().toString()
(4 answers)
Closed 3 years ago.
I'm having the date in this pattern EEEEE MMMMM yyyy HH:mm:ss.SSSZ, I would like to convert this to yyyy-MM-dd format in java, I tried below approach but I'm getting this exception java.text.ParseException: Unparseable date:
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "Sun Oct 01 00:00:00 EDT 2017";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
Can someone please help me to resolve this?
Thanks.
From java-8 you can use the ZonedDateTime with pattern of your input date which is EEE MMM dd HH:mm:ss zzz yyyy
String dateInString = "Sun Oct 01 00:00:00 EDT 2017";
ZonedDateTime time = ZonedDateTime.parse(dateInString,DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy"));
System.out.println(time.toLocalDate()); //2017-10-01
By default LocalDate is without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.
You've defined your formatter as the concept 'date, month, year', and then you try to ask it to parse a string that isn't in this format at all. You need to make a formatter that can format Sun Oct 01 00:00:00 EDT 2017, and dd-MMM-yyyy obviously isn't it. The javadoc of SimpleDateFormat will tell you what combination of letters you need to use.
Once you've got that, it's easy: parse with this new formatter, then call .format with your old one (the dd-MMM-yyyy one).
You can't use the same formatter for both parsing and formatting. See this answer: https://stackoverflow.com/a/999191
You double-create the DateFormat one parse and once to format
DateFormat dfParse = new SimpleDateFormat("EEEEE MMMMM yyyy HH:mm:ss.SSSZ");
DateFormat dfFormat = new SimpleDateFormat("yyyy-MM-dd");
dfFormat.format(dfParse.parse("Sun Oct 01 00:00:00 EDT 2017"))

How to Parse Date in java [duplicate]

This question already has answers here:
How can I convert Date.toString back to Date?
(5 answers)
Closed 4 years ago.
Unparseable date: "Tue Jul 03 16:59:51 IST 2018" Exception
String date="Tue Jul 03 16:59:51 IST 2018";
i want to parse it.
My code is
SimpleDateFormat newformat=SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
Date d=newformat.parse(date);
You are using old and quite problematic classes, especially Date.
For your example, perhaps consider using LocalDateTime
String date = "Tue Jul 03 16:59:51 IST 2018";
DateTimeFormatter format = DateTimeFormatter
.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(date, format);
Since your pattern has to match the string you want to parse you need to adjust the pattern as following:
EEE MMM dd HH:mm:ss z yyyy
Infos gathered from the docs.
You should also use the new java.time API introduced with Java 8.
String s = "Tue Jul 03 16:59:51 IST 2018";
//Java 7 way
SimpleDateFormat newformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date d = newformat.parse(s);
//Java 8 way
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime dateTime = LocalDateTime.parse(s, formatter);

Convert String to Date error [duplicate]

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);

Read date from file using java [duplicate]

This question already has answers here:
Java - Unparseable date
(3 answers)
Closed 8 years ago.
I'm triyng to make a program read a date from a txt file into a Date object. Here is the code I'm using :
reader = new FileReader(fich);
rd = new Scanner(reader);
DateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
Date tmpdate= formatter.parse(rd.nextLine());
And this is the line I want to read:
Fri Feb 20 01:23:35 GMT 2015
and then it shows up this error:
Exception in thread "main" java.text.ParseException: Unparseable date: "Fri Feb 20 01:23:35 GMT 2015"
what is wrong?
It seems that Fri and Feb are not correct names in your locale. Try specifying one where they are correct like Locale.ENGLISH.
So instead of
new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy");
use
new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZ yyyy", Locale.ENGLISH);

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