3

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

Luca_Scorpion
  • 371
  • 4
  • 15
  • 10
    What is your locale? The textual day and month representation depend on that. – cheffe Nov 03 '15 at 10:06
  • 2
    Try to pass in the "en-us" Locale to the constructor to lock down that part. – Thilo Nov 03 '15 at 10:10
  • 2
    You can try @cheffe suggestion like this `SimpleDateFormat("EEE", Locale.ENGLISH)`. By the way I've tested your code with your String and it works fine. Language in my system English – Manos Nikolaidis Nov 03 '15 at 10:10
  • @cheffe Good point, my date and time format is set to Dutch, and you just fixed the problem :) Thanks! – Luca_Scorpion Nov 03 '15 at 10:11

1 Answers1

1

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

Luca_Scorpion
  • 371
  • 4
  • 15