Skip to content

Instantly share code, notes, and snippets.

@cb372
Created May 4, 2016 15:27
Show Gist options
  • Save cb372/5d1460433b4697e2863d6553c803a926 to your computer and use it in GitHub Desktop.
Save cb372/5d1460433b4697e2863d6553c803a926 to your computer and use it in GitHub Desktop.
Confusing default behaviour of Joda DateTime parsing
/*
* By default Joda will parse an ISO 8601 string into a DateTime
* with its timezone set to the _local_ timezone, not the one specified in the ISO string.
* You can fix this by calling `withOffsetParsed()` on your formatter.
*/
scala> import org.joda.time.format._
import org.joda.time.format._
scala> new DateTime("2016-04-06T15:22:01Z")
res0: org.joda.time.DateTime = 2016-04-06T16:22:01.000+01:00 // creates a DateTime in the local timezone (GMT+1)
scala> ISODateTimeFormat.dateTimeNoMillis().parseDateTime("2016-04-06T15:22:01Z")
res1: org.joda.time.DateTime = 2016-04-06T16:22:01.000+01:00 // same result
scala> ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed().parseDateTime("2016-04-06T15:22:01Z")
res2: org.joda.time.DateTime = 2016-04-06T15:22:01.000Z // ah, that's what I wanted!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment