Jackson-databind: @JsonFormat formatted date data appear time zone problem

Created on 13 Jun 2016  路  4Comments  路  Source: FasterXML/jackson-databind

DOC is said to default on the use of TimeZone.getDefault (), but the actual is not the case. Before it is written directly on the fixed value. But now the project needs to be internationalized and deployed to different countries, so the time zone must take the time to run the environment. However, with the JSON @JsonFormat into the string after the time and running environment of the default time zone for a long time.

Most helpful comment

You guessed it! I said that the DOC is javadocs. I am sorry, I am a chinese. English is not very good.
I'll try to describe it in detail.
For example:
public class Demo {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private java.util.Date uploadTime;
//ellipsis Getter銆丼etter
}
This class is serialized to json, it was 8 hours (because in China, the time zone is GMT+8) with my local running environment.
I wrote this before: @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")锛孴his is no problem.
But now there is a project to give users in a number of countries to use. So time zones can't be fixed.
So it can only be written in this way: @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")锛孋an only take the JVM operating environment of the time zone.

I read the javadocs:http://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonFormat.html#timezone()

TimeZone to use for serialization (if needed). Special value of DEFAULT_TIMEZONE can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system defaults (TimeZone.getDefault()) unless explicitly set to another locale.

I understand that it is the default time zone using the JVM runtime environment, but it is not the case.
After my study found the use of objectMapper.setTimeZone (TimeZone.getDefault) after solving the problem.
Since I was using the SpringMVC, so I changed the spring configuration file as follows:
<bean name="timeZone" class="java.util.TimeZone" factory-method="getDefault"></bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="timeZone" ref="timeZone"></property>
</bean>
</property>
</bean>
This configuration can solve the problem. But I think, even if it is not such a setting should also default to use the JVM operating environment of the time zone.

All 4 comments

I am sorry but I do not understand what you are saying here. I am guessing "DOC" means documentation (javadocs); but from that on, what exactly is not working. What kind of Java objects are you using, and what is relevant JSON being read and/or written? How is this working different from your expecations?

You guessed it! I said that the DOC is javadocs. I am sorry, I am a chinese. English is not very good.
I'll try to describe it in detail.
For example:
public class Demo {
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
private java.util.Date uploadTime;
//ellipsis Getter銆丼etter
}
This class is serialized to json, it was 8 hours (because in China, the time zone is GMT+8) with my local running environment.
I wrote this before: @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")锛孴his is no problem.
But now there is a project to give users in a number of countries to use. So time zones can't be fixed.
So it can only be written in this way: @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")锛孋an only take the JVM operating environment of the time zone.

I read the javadocs:http://fasterxml.github.io/jackson-annotations/javadoc/2.7/com/fasterxml/jackson/annotation/JsonFormat.html#timezone()

TimeZone to use for serialization (if needed). Special value of DEFAULT_TIMEZONE can be used to mean "just use the default", where default is specified by the serialization context, which in turn defaults to system defaults (TimeZone.getDefault()) unless explicitly set to another locale.

I understand that it is the default time zone using the JVM runtime environment, but it is not the case.
After my study found the use of objectMapper.setTimeZone (TimeZone.getDefault) after solving the problem.
Since I was using the SpringMVC, so I changed the spring configuration file as follows:
<bean name="timeZone" class="java.util.TimeZone" factory-method="getDefault"></bean>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="timeZone" ref="timeZone"></property>
</bean>
</property>
</bean>
This configuration can solve the problem. But I think, even if it is not such a setting should also default to use the JVM operating environment of the time zone.

Ok. Thank you for the explanation!

In this case, javadocs are wrong: the default TimeZone in use is UTC, and not whatever JVM has.
I will try to update the documentation, since this is the desired behavior and has been so for Jackson 2.x.
Since it is the current behavior change to the setting would potentially cause problem existing users; and I am also not convinced that use of local settings would be better defaulting.

Thank you for reporting the discrepancy with javadocs.

Improved javadocs for jackson-annotations (for JsonFormat) and ObjectMapper / BaseSettings (for jackson-databind).

Was this page helpful?
0 / 5 - 0 ratings