SimpleDateFormat "Unparseable date" Exception

Wade picture Wade · Oct 7, 2011 · Viewed 44.5k times · Source

I am trying to parse datetime string with SimpleDateFormat.parse() but I keep receiving Unparseable date exceptions.

Here is the date format I am trying to parse: 2011-10-06T12:00:00-08:00

Here is the code I am using:

try {
    String dateStr = "2011-10-06T12:00:00-08:00";
    SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMM d, yyyy");
    Date date = dateParser.parse(dateStr);
    System.out.println(dateFormatter.format(date));         
} catch(Exception e) {
    System.out.println(e.getMessage());
}

Which returns this error: java.text.ParseException: Unparseable date: "2011-10-06T12:00:00-08:00"

As far as I know this is the correct way to use the SimpleDateFormat class but I'm not fluent in Java so I could be mistaken. Any one know what my issue is?

Answer

Skip Head picture Skip Head · Oct 7, 2011

The timezone should be GMT-08:00 or -0800 (as Madcore Tom said). See Java docs.

In Java 7 you can use "yyyy-MM-dd'T'HH:mm:ssX"