Search code examples
javadatesimpledateformatparseexception

Java: simple string to date parsing fails with exception


This is my method:

  @Test
  public void convertUpdateDateToDateTest() {
    String updateDate = "11-Dec-2015";

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");

    try {
      Date date = formatter.parse(updateDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

I don't understand why this is not working.

Exception:

java.text.ParseException: Unparseable date: "11-Dec-2015"

Solution

  • You can have a Locale different then English specified as your Java default one in which case parsing won't recognize English month names.

    Try this:

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH);
    

    IDEONE DEMO