[英]ISO8601 to DateTime with time zone information preserved
下面是包含时区信息的ISO8601日期字符串的反序列化。 请注意,时区信息丢失:
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
时区信息(UTC偏移)被序列化,如ISO8601字符串末尾的+ +03:00
和+02:00
,但在反序列化后丢失。 正如你所看到的date2
DateTime对象,这是我预计的副本date1
对系统的UTC偏移,而非+02:00
,其中date1
了。
如何反序列化ISO8601字符串以保留UTC偏移量?
您正在使用的构造函数, new DateTime(Object instant)
,(实际传递给BaseDateTime
)不会解析 ,而是转换给定的对象(在您的情况下,为String
)。
长话短说,它使用默认时区:
Instant
并从ConverterManager
请求InstantConverter StringConverter
上调用getInstantMillis()
DateTimeFormatter
,但是它不是parse
而是调用parseMillis()
。 parseMillis
在默认时区中返回一个日期。 改为使用DateTime.parse
:
DateTime date2 = DateTime.parse(date1.toString());
// 2013-09-22T19:21:48.461+02:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.