2

All,

I know I have asked a similar question on parsing an ISO8601 date string into a Date using Java before, but this is a more specific problem using the SimpleDateFormat class.

I have read the article Wiki ISO8601 Date.

I have been supplied an XML file from a customer which has a date and time with the following format:

2012-08-24T12:15:00+02:00

According to the Wiki article this is valid which is fair enough.

Given the following code to parse this string, a ParseException is thrown with the message "Unparseable date: "2012-08-24T12:15:00+02:00"".

String inputDate = "2012-08-24T12:15:00+02:00";
String format = "yyyy-MM-dd'T'HH:mm:ssz";

SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(inputDate);

The problem is with the colon in the timezone specifier. +02:00 in the timezone causes the exception to be thrown. +0200 works fine.

Question is, is it possible to parse this type of string with the SimpleDateFormat?

Thanks

Andez

1

2 Answers 2

4

No, using SimpleDateFormat, parsing this date is not possible (at least not in jdk 6 or lower). We had to write our own adapter for this ourselves.

Note, since this format is a valid part of XML Schema, you can use the DatatypeConverter.parseDateTime() method to parse this date.

4
  • +1 Better than using SimpleDateFormat
    – Damo
    Mar 13, 2013 at 5:52
  • This is throwing an IllegalArgumentException for me when I try to parse "2015-10-19T18:58:23.000+0000" Oct 22, 2015 at 17:00
  • @KimballRobinson - probably because that does not fit the xsd dateTime format (you are missing the ':' in the timezone offset).
    – jtahlborn
    Oct 22, 2015 at 17:49
  • @jtahlborn, I'm getting time stamps from the Salesforce REST api, so I don't control the format. I am using Joda Time's library to parse the timestamps now. Oct 23, 2015 at 13:44
1

The best answer I have found is on this question.

Converting ISO 8601-compliant String to java.util.Date

Quote: The easier solution is possibly to use the data type converter in JAXB, since JAXB must be able to parse ISO8601 date string according to the XML Schema specification. javax.xml.bind.DatatypeConverter.parseDateTime("2010-01-01T12:00:00Z") will give you a Calendar object and you can simply use getTime() on it, if you need a Date object.

Not the answer you're looking for? Browse other questions tagged or ask your own question.