Search code examples
javadate

Unparseable date


I have a string-date "31-Dec", and pattern "dd-MMM". And the next code

DateFormat formatter = new SimpleDateFormat(pattern);
formatter.setTimeZone(timeZone);
formatter.parse(input);

generates exception

java.text.ParseException: Unparseable date: "31-Dec"
    at java.text.DateFormat.parse(DateFormat.java:337)....

What did I do wrong?

Thanks!


Solution

  • One problem could be that your Locale is not english. Try this:

    DateFormat formatter = new SimpleDateFormat("dd-MMM", Locale.ENGLISH);
    try {
        System.out.println(formatter.parse("31-Dec"));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    

    This returns for me:

    Thu Dec 31 00:00:00 CET 1970

    As you are missing a year in your date string, you see that it automatically inserts 1970 as year.