I am very skeptical about considering Z as literally. char Z matters, namely zero offset. The Joda-Time documentation version 1.6 talks about this code:
String timestamp = "2014-09-23T23:03:11Z"; DateTime dt = ISODateTimeFormat.dateTimeNoMillis().parseDateTime(timestamp).withZone(DateTimeZone.UTC); System.out.println(dt);
Returns a formatter that combines the full date and time without milliseconds separated by "T" (yyyy-MM-dd'T'HH: mm: ssZZ). The time zone offset is βZβ for zero and the shape is βΒ± HH: mmβ for non-zero.
Now let's look at the following four alternatives (explicitly tested with version 1.6.2):
String timestamp = "2014-09-23T23:03:11Z"; DateTimeZone utc = DateTimeZone.UTC; DateTime dt1 = ISODateTimeFormat.dateTimeNoMillis().parseDateTime(timestamp).withZone(utc); System.out.println(dt1); // 2014-09-23T23:03:11.000Z (OK) DateTime dt2 = new DateTime(timestamp, utc); System.out.println(dt2); // 2014-09-23T23:03:11.000Z (OK) DateTime dt3 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").parseDateTime(timestamp).withZone(utc); System.out.println(dt3); //2014-09-23T21:03:11.000Z (WRONG!!!) DateTime dt4 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZZ").parseDateTime(timestamp).withZone(utc); // exception: Invalid format: "2014-09-23T23:03:11Z" is malformed at "Z"
Conclusion: The other answers that have so far treated Z as a literal are incorrect because the input is processed in the local time zone, and not with an offset of UTC + 00: 00. Use the constructor or the concrete class IsoDateTimeFormat (I would prefer the latter for clarity).
About the exception: This is a bug fixed with version 2.0, see the release notes . You better upgrade your library version.
Allow 'Z' and 'ZZ' in template formats to parse 'Z' as '+00: 00' [2827359]
source share