javasimpledateformat

SimpleDateFormat ParseException: Unparseable date Error


I'm parsing this date format from XML: "2011-12-06T07:41:14.016+00:00", and I'm getting this error:

*W/System.err(574): java.text.ParseException: Unparseable date: "2011-12-06T07:41:14.016+00:00"

I'm certain it's the formatting statement I'm using, but I can't figure out what it SHOULD be.

Here's the statement I'm using:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ:ss");

I get how to create a format for this part: "2011-12-06T07:41:14....", it's this other part :=> ".016+00:00" that's throwing me for a loop.

I've looked for answers here already: Android SimpleDateFormat Page, and here Oracle SimpleDateFormat Page, but I fear I'm missing something fundamental....

What is the proper format statement for that particular date format?


Solution

  • The "Z" pattern matches +0000 and not +00:00 so if you remove the last ":" before you parse then it will work.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ");
    try {
      Date myDate = sdf.parse( "2011-12-06T07:41:14.016+00:00".replaceAll( "([0-9\\-T]+:[0-9]{2}:[0-9.+]+):([0-9]{2})", "$1$2" ) );
      System.out.println( myDate );
    } catch (ParseException e) {
      e.printStackTrace();
    }