ISO8601 to DateTime with time zone information preserved

╄→尐↘猪︶ㄣ 提交于 2019-12-22 06:17:10

问题


Below is a deserialization of an ISO8601 date string that contains time zone information. Notice that the time zone information is lost:

scala> val date1 = new DateTime().withZone(DateTimeZone.forID("Europe/Berlin"))
date1: org.joda.time.DateTime = 2013-09-22T18:42:15.348+02:00

scala> date1.getZone()
res45: org.joda.time.DateTimeZone = Europe/Berlin

scala> val date2 = new DateTime(date1.toString())
date2: org.joda.time.DateTime = 2013-09-22T19:42:15.348+03:00

scala> date2.getZone()
res46: org.joda.time.DateTimeZone = Europe/Vilnius

scala> date1.getZone() == date2.getZone()
res47: Boolean = false

Time zone information (UTC offset) is serialized, as in +03:00 and +02:00 at the end of the ISO8601 strings, but it is lost after deserialization. As you can see the date2 DateTime object, which I expected to be a copy of date1 has the system's UTC offset instead of +02:00, which date1 had.

How do I deserialize an ISO8601 string as to preserve the UTC offset?


回答1:


The constructor you are using, new DateTime(Object instant), (actually passed through to BaseDateTime) doesn't parse, instead it converts the given object (in your case, a String).

Long story short, it uses the default time zone:

  1. The constructor considers the passed parameter an Instant and requests an InstantConverter from ConverterManager
  2. The constructor calls getInstantMillis() on that StringConverter
  3. That method actually does use a standard ISO 8601 DateTimeFormatter, however instead of parse it calls parseMillis().
  4. parseMillis, as you can see from the javadocs, returns a date in the default time zone.

Use DateTime.parse instead:

DateTime date2 = DateTime.parse(date1.toString());
// 2013-09-22T19:21:48.461+02:00


来源:https://stackoverflow.com/questions/18946311/iso8601-to-datetime-with-time-zone-information-preserved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!