Home > other >  Java ParseException: Unparseable date When Date Should be Parseable
Java ParseException: Unparseable date When Date Should be Parseable

Time:07-12

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

CodePudding user response:

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")
  • Related