-4

The following code:

String time = "Mon May 07 00:31:29 GMT+02:00 2018";
Date parsedDate = new SimpleDateFormat("E MMM dd HH:mm:ss zXXX yyyy", Locale.GERMANY).parse(time);

throws the following exception, instead of parsing the datestring:

java.text.ParseException: Unparseable date: "Mon May 07 00:31:29 GMT+02:00 2018"

I found the format string by trying different formatter combinations on this site and the one I came up with (E MMM dd HH:mm:ss zXXX yyyy) seems to give the right result (on the site).

However, when used in my code it throws the given exception.

Do I have a misconception about SimpleDateFormat or is my format string wrong?

F.M.F.
  • 1,929
  • 3
  • 23
  • 42
  • 1
    What `not working` means? Questions are encouraged to provide a [Minimum Complete and Verifiable Example](https://stackoverflow.com/help/mcve), try to improve yours. – LMC May 06 '18 at 23:44
  • 1
    Is `May` spelt `May` in German? With a little bit of testing, for `Locale.GERMANY`, I get `Mai` for the month of `May` – MadProgrammer May 06 '18 at 23:48
  • I also can't get the combination of `zX` to work, but `z` seems to work fine by it self – MadProgrammer May 06 '18 at 23:51
  • Okay, after some mucking around, either your input needs to be `Mo Mai 07 00:31:29 GMT+02:00 2018` (with the format of `E MMM dd HH:mm:ss z yyyy`) or you need to drop `Locale.GERMANY` – MadProgrammer May 06 '18 at 23:54
  • [When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. Click this comment to find out how to provide what we need to help you.](https://stackoverflow.com/help/mcve) –  May 06 '18 at 23:57
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  May 06 '18 at 23:57
  • I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. May 07 '18 at 07:51
  • Possible duplicate of [Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical](https://stackoverflow.com/questions/46285384/getting-error-java-text-parseexception-unparseable-date-at-offset-0-even-if) – Ole V.V. May 07 '18 at 08:04
  • Sorry for the bad question. – F.M.F. May 07 '18 at 08:23

1 Answers1

4

Either you need to use...

String time = "Mo Mai 07 00:31:29 GMT+02:00 2018";
Date parsedDate = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.GERMANY).parse(time);

Or

String time = "Mon May 07 00:31:29 GMT+02:00 2018";
Date parsedDate = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy").parse(time);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366