Unfortunately, the time zone formats available to SimpleDateFormat (Java 6 and earlier) are not ISO 8601 compliant. SimpleDateFormat understands time zone strings like "GMT+01:00" or "+0100", the latter according to RFC # 822.
Even if Java 7 added support for time zone descriptors according to ISO 8601, SimpleDateFormat is still not able to properly parse a complete date string, as it has no support for optional parts.
Reformatting your input string using regexp is certainly one possibility, but the replacement rules are not as simple as in your question:
Some time zones are not full hours off UTC, so the string does not necessarily end with ":00".
ISO8601 allows only the number of hours to be included in the time zone, so "+01" is equivalent to "+01:00"
ISO8601 allows the usage of "Z" to indicate UTC instead of "+00:00".
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.
You could probably use Joda-Time as well, but I don't know why you should bother with that.
The JAXB-solution is a really creative approach! It works as well, I have tested it with my sample. However, for whoever faces the problem and is allowed to use JodaTime, I would advise to use it, since it feels more natural. But your solution requires not additional libraries (at least with Java 6).
Here's the reverse: Calendar c = GregorianCalendar.getInstance();c.setTime(aDate);return javax.xml.bind.DatatypeConverter.printDateTime(c);
Actually it's not so simple b/c you have to initialize the jaxb datatypeConverter. I ended up using DatatypeFactory myself as DataTypeConverterImpl did internally. What a headache.
@jarnbjo you are the first and only person I have encountered who prefers the standard, pre 1.8, java date classes, over joda-time. I find joda-time a literal joy to use, especially when compared to the standard api which is an abomination.
Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).