How to set default timezone for Joda DateTime.parse ()

I want to use Joda Time to parse a time that does not have an explicit time zone with a specific default zone (e.g. UTC), but use an explicit zone if one is present. For instance:

parse("2014-02-11T12:00:00")       // Should be UTC
parse("2014-02-11T12:00:00+02:00") // Should be +2 hours

Everything I tried has some problems:

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 behavior withZone()and withOffsetParsed()at the same time, but they override each other.

I do not want to change the default time zone for the entire JVM.

+3
source share
1 answer

Found out the answer when I introduced the question. Since fast Google did not find the answer, I decided that I would publish it. Decision:

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

All Articles