0

I am getting an Unparseable date exception for a date I would expect to be parseable.

java.text.ParseException: Unparseable date: "03 Sep 18" at java.base/java.text.DateFormat.parse(DateFormat.java:399)

From the following code:

    Date aired = null;
    try {
        aired = new SimpleDateFormat("dd MMM yy").parse(episodeData.get(3));
    } catch (final ParseException e) {
        LOGGER.info("Issue converting date");
        e.printStackTrace();
    }
    ep.setAirDate(aired);
mhollander38
  • 775
  • 3
  • 11
  • 22
  • Could you provide info about ```episodeData.get(3)```? – AddeusExMachina Jul 12 '22 at 09:56
  • @AddeusExMachina You can infer from the Exception message what it is – Michael Jul 12 '22 at 10:02
  • I strongly recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 12 '22 at 17:30

1 Answers1

2

Your default Locale is presumably non-English. You can verify with this what your Locale is.

System.out.println(Locale.getDefault());

For me, in the UK, this prints en_GB.

Because Java is presumably using non-English, your native name for the 9th month is not "September", and so "Sep" fails to parse.

You can fix it by specifying an English Locale in the SimpleDateFormat, rather than relying on the default:

new SimpleDateFormat("dd MMM yy", Locale.UK).parse("03 Sep 18")

US locale should work. new SimpleDateFormat("dd MMM yy", Locale.US).parse("03 Sep 18")) does not throw an exception for me. That said, Locale info can change between Java versions. In fact the short form of September did change in Java 17. Something to be aware of.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Michael
  • 41,989
  • 11
  • 82
  • 128