How to set the default time zone for Joda's DateTime.parse()

java, jodatime

Solution

Figured out the answer while I was typing the question. Since a quick Google didn't find the answer, I figured I'd post it. The solution is:

ISODateTimeFormat.dateTimeParser()
        .withChronology(ISOChronology.getInstance(timeZone))
        .withOffsetParsed()
        .parseDateTime(...);

Problem

I want to use Joda Time to parse times that have no explicit time zone with a particular default zone (UTC for example), but use the explicit zone if present. For example: ``` parse("2014-02-11T12:00:00") // Should be UTC parse("2014-02-11T12:00:00+02:00") // Should be +2 hours ``` Everything I've tried has some problem: ``` DateTime.parse("2014-02-11T12:00:00") // Gives the server time zone, -5 hours ISODateTimeFormat.dateTimeParser() .withZone(DateTimeZone.UTC) .parseDateTime("2014-02-11T12:00:00+02:00"); // Gives UTC ``` Basically I want the behaviour of `withZone()` and `withOffsetParsed()` at the same time, but they override each other. I don't want to change the default time zone of the whole JVM.

Original source