Search code examples
javasimpledateformatstring-parsing

Parsing date with SimpleDateFormat


Why does the following give an error:

    DateFormat formatter1 = new SimpleDateFormat("E, MMM d");
    formatter1.setTimeZone(TimeZone.getTimeZone("America/New_York"));

    formatter1.parse("Tue, Nov 26");

I don't get why it isn't working.


Solution

  • You should set a Locale to your formatter where months are spelt in English, otherwise it's using your default Locale :

    SimpleDateFormat(String pattern)
    

    Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default locale.

    I.e :

    DateFormat formatter1 = new SimpleDateFormat("E, MMM d", Locale.US);