joda time ISO DateTime formatting

pichsenmeister picture pichsenmeister · Jun 20, 2014 · Viewed 9.2k times · Source

I'm using joda time to format my ISO Date input string, but I'm getting an exception that my ISO Date is malformed:

Invalid format: "2014-06-20T11:41:08+02:00" is malformed at "+02:00"

This is my code:

val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime.withZone(DateTimeZone.getDefault)
val date: DateTime = formatter.parseDateTime("2014-06-20T11:41:08+02:00")

What's wrong here?

Answer

Norbert Radyk picture Norbert Radyk · Jun 20, 2014

The error comment is slightly misleading here, as Joda formatter you derive from ISODateTimeFormat expects the millisecond part of the date/time string to be present, therefore the following will work fine:

val formatter: DateTimeFormatter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.getDefault())
val date: DateTime = formatter.parseDateTime("2014-06-20T11:41:08.0+02:00")