使用Jackson(Joda-Time模块)进行DateTime反序列化的默认时区 [英] Default timezone for DateTime deserialization with Jackson (Joda-Time module)

查看:1257
本文介绍了使用Jackson(Joda-Time模块)进行DateTime反序列化的默认时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是关于 Joda-Time 日期时间 使用 Jackson 的/github.com/FasterXML/jackson-datatype-joda\"rel =noreferrer> jackson-datatype-joda模块。是否有一个默认时区,日期字符串将被反序列化?如果是这样,它是什么?是 UTC

This question is about deserialization to Joda-Time DateTime using jackson-datatype-joda module for Jackson. Is there a default timezone that date strings will be deserialized into? If so, what is it? Is it UTC?

我需要问这个,因为杰克逊的文档并不是特定的Joda-Time DateTime。我发现这篇文章( http://wiki.fasterxml.com/JacksonFAQDateHandling )杰克逊将假设 GMT 作为反序列化为的默认时区java.util.Date java.util.Calendar 。但是,本文档中没有提及Joda-Time数据类型。另外,我特别需要字符串使用 UTC 时区反序列化为 DateTime 对象,而不是GMT:尽管这两个区域非常相似,但是一些小的差异,因此GMT对我来说是不可行的。

I need to ask this because the Jackson documentation is not specific for Joda-Time DateTime. I found in this article (http://wiki.fasterxml.com/JacksonFAQDateHandling) that Jackson will assume GMT as the default time zone for deserializing into java.util.Date or java.util.Calendar. However, there is no mention of Joda-Time data types in this document. In addition, I specifically need strings to deserialize into DateTime objects using the UTC timezone, not GMT: although these two zones are very similar, there are some small differences and therefore GMT will not be feasible for me.

谢谢。

推荐答案

DateTimeDeserializer 显示它使用 DeserializationContext 在反序列化期间由 ObjectMapper 提供。如果你看一下 ObjectMapper API,你会看到有设置时区的方法:

The source code for DateTimeDeserializer shows it uses the timezone from DeserializationContext which is provided by ObjectMapper during deserialization. If you look at ObjectMapper API, you will see there is method for setting the timezone:

public ObjectMapper setTimeZone(TimeZone tz)

因此您可以使用此方法配置 ObjectMapper 并将时区设置为正确的时区。

Thus you can use this method to configure your ObjectMapper and set the timezone to the correct one.

对于默认值,似乎Javadoc说了一件事,但代码显示另一个。

For what concerns the default value, it seems the Javadoc says one thing, but the code shows another.

Javadoc for ObjectMapper.setTimeZone(TimeZone tz)

Javadoc for ObjectMapper.setTimeZone(TimeZone tz):

/**
  * Method for overriding default TimeZone to use for formatting.
  * Default value used is {@link TimeZone#getDefault()}.
  */

但是,代码明确设置了时区:

However, the code sets the timezone explicitly on:

protected final static BaseSettings DEFAULT_BASE = new BaseSettings(
    ...
    // TimeZone.getDefault()
    TimeZone.getTimeZone("GMT"),
    ...

所以,显然,它实际上使用的是GMT,而不是默认的JVM默认值。

So, apparently, it actually uses GMT, and not the default JVM default.

我想说最好的选择可能不依赖于此并自行设置 ObjectMapper.setTimeZone(TimeZone tz)

I would say that probably the best choice would be not relying on this and set it by yourself on ObjectMapper.setTimeZone(TimeZone tz).

这篇关于使用Jackson(Joda-Time模块)进行DateTime反序列化的默认时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆