Search code examples
javadatetimetimezonelocaledatetime-format

Parser Exception for format EEEE, MMMM d, YYYY h:mm:ss a z


I'm getting parser exception on trying to parse string value:

"Thursday, July 27, 2006 10:10:02 PM PST" 

To format:

"EEEE, MMMM d, YYYY h:mm:ss a z"

This is the program sample:

DateTime.parse("Thursday, July 27, 2006 10:10:02 PM PDT", DateTimeFormat.forPattern("EEEE, MMMM d, yyyy h:mm:ss a z"));

And this is the error message:

Invalid format: "Thursday, July 27, 2006 10:10:02 PM PDT" is malformed at "PDT"

this is my sample program

String str = "Thursday, July 27, 2006 10:10:02 PM PDT"; 
DateTimeFormatter formatterDateTime = DateTimeFormat.forPattern("EEEE, MMMM d, YYYY h:mm:ss a z");
try{
    DateTime dt = DateTime.parse(str, formatterDateTime);
}catch(Exception ex)
{
    System.out.println(ex.getMessage());
}

Solution

  • From the JodaTime docs:

    Zone names: Time zone names ('z') cannot be parsed.

    However SimpleDateFormat does support parsing of timezones.

    SimpleDateFormat format = new SimpleDateFormat("EEEE, MMMM dd, YYYY h:mm:ss aa zzz");
    Date date = format.parse("Thursday, July 27, 2006 10:10:02 PM PST");