Search code examples
javasimpledateformatparseexception

SimpleDateFormat ParseException: Unparseable date


I want to parse the following date:

24 07 2017 3:47:57 AM

with the following format:

SimpleDateFormat df2 = new SimpleDateFormat("dd MM yyyy hh:mm:ss a");
df2.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
    df2.parse(dateStr + " "+ sunrise);
}catch(ParseException e){
    e.printStackTrace();
}

But I get the following error:

java.text.ParseException: Unparseable date: "24 07 2017 3:47:57 AM"

Solution

  • One possibility is that your default Locale has different symbols for AM/PM. When constructing a date format you should always supply a Locale unless you really want to use the system's default Locale, for example:

    SimpleDateFormat df2 = new SimpleDateFormat("dd MM yyyy hh:mm:ss a", Locale.US)