我想使用Joda Time来解析没有具有特定默认区域(例如UTC)的显式时区的时间,但是如果存在则使用显式区域.例如:
parse("2014-02-11T12:00:00") // Should be UTC
parse("2014-02-11T12:00:00+02:00") // Should be +2 hours
Run Code Online (Sandbox Code Playgroud)
我尝试的一切都有一些问题:
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
Run Code Online (Sandbox Code Playgroud)
基本上我想要withZone()和withOffsetParsed()同时的行为,但他们互相覆盖.
我不想更改整个JVM的默认时区.
在输入问题时找出答案.由于快速谷歌没有找到答案,我想我会发布它.解决方案是:
ISODateTimeFormat.dateTimeParser()
.withChronology(ISOChronology.getInstance(timeZone))
.withOffsetParsed()
.parseDateTime(...);
Run Code Online (Sandbox Code Playgroud)