0

My SimpleDateFormat looks like this:

Date expiryDate = null;
try {
    expiryDate = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss zzz")
      .parse(paramValue.trim());
} catch (ParseException e) {
    e.printStackTrace();
}

And when I try to parse the following Date: "Tue, 01-Jan-2036 08:00:01 GMT", I will receive java.text.ParseException: Unparseable date: "Tue, 01-Jan-2036 08:00:01 GMT"

I tried differnt dates in the same format, maybe I'm missing something or there is something special I don't know

CodeFox
  • 447
  • 4
  • 12
  • 1
    Seems to work okay for me – MadProgrammer Jun 13 '18 at 08:00
  • I just tried parsing `"Tue, 01-Jan-2036 08:00:01 GMT"` with `new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm:ss zzz")` and it worked fine. – khelwood Jun 13 '18 at 08:00
  • 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/). – Ole V.V. Jun 13 '18 at 08:01
  • 1
    Does not work for me actually. Maybe an issue with Locale? – Ben Jun 13 '18 at 08:01
  • 1
    Changed Locale to `Locale.CANADA` and it works without problems. So definitely a problem with certain Locales. – Ben Jun 13 '18 at 08:01
  • 1
    FYI: it's a locale issue as suggested by Ben, I'm using US locale now, in standart envoirnment on my application GERMAN is used – CodeFox Jun 13 '18 at 08:06
  • 2
    Note that 'hh' in time section is 12 hour format, for 24 hr format use 'HH', so parsing 15:00:00 with 'hh' will throw parse exception – jr593 Jun 13 '18 at 08:26
  • @jr593 You are completely correct that lowercase `hh` is wrong. However, unfortunately a `SimpleDateFormat` with standard settings does not throw the `ParseException` that we would expect. It’s just one example out of many of how “surprisingly" it may behave and how troublesome it is. – Ole V.V. Jun 15 '18 at 08:25

0 Answers0