Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleDateFormat inconsistent parsing error [closed]

The sample code speaks for itself:

private void parse() throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy");

        Date started = sdf.parse("Sep/22/2004");
        // this triggers: java.text.ParseException: Unparseable date: "May/23/2010"
        Date ended = sdf.parse("May/23/2010");
}

Not sure what more I can add to this. I'm trying to parse "MMM/dd/yyyy" dates, and I get inconsistent exception behavior. It feels like I'm missing something obvious.

like image 387
habitats Avatar asked Oct 04 '22 19:10

habitats


1 Answers

It was of course locale related. Add the following:

sdf.setDateFormatSymbols(DateFormatSymbols.getInstance(Locale.ENGLISH));

Or alternatively simply declare it with locale:

SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yyyy", Locale.ENGLISH);
like image 156
habitats Avatar answered Oct 07 '22 18:10

habitats