5

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?

1

3 Answers 3

5

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

parsing a time without AM/PM using 12 hour format (hh) is not the best idea
There still is a small secundary bug from the original question: hh (12 hours) should be HH (24 hours) otherwise you will get wrong PM times.
2
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"    2001-07-04T12:08:56.235-0700

The :ss at the end looks incorrect

Comments

2

As one of the answers in the question I linked shows, you can use

String string = "2011-12-06T07:41:14.016+00:00";
Calendar cal = DatatypeConverter.parseDateTime(string);

What's the best way to parse an XML dateTime in Java?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.