Search code examples
javasimpledateformat

SimpleDateFormat Error


I am trying to convert a date in java, but I am missing something. After spent several hours in this I couldn't figure out the solution:

SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MMM-yy");
Date date2 =  formatter2.parse("09-Feb-13");  // throws error!
System.out.println(date2);

Solution

  • If you don't provide a Locale to the formatter, it uses your default one which apparently doesn't spell months in English.

    SimpleDateFormat(String pattern)

    Constructs a newSimpleDateFormatusing the specified non-localized pattern and theDateFormatSymbolsandCalendarfor the user's default locale.

    Specify one that does :

    SimpleDateFormat formatter2 = new SimpleDateFormat("dd-MMM-yy", Locale.UK);