jackson-databind: Jackson adjusts date even though date format doesn't contain timezone
For some reason jackson adjusts the date, even though the date format doesn’t contain the timezone.
E.g.
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
Binds a string of “2018-02-15T11:43:00” to a Date of “2018-02-15 12:43:00” on my local system. Shouldn’t jackson be neutral on the timezone if it is not explicitly provided? Is it possible to configure the current version of jackson to follow the expected behavior (Date of “2018-02-15 11:43:00” in above case)
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 16 (8 by maintainers)
Just for people who are searching the same issue and have reached this ticket, if your date is such like a birthday, and you don’t care about the timezone and timestamp at all, you’d better use LocalDateTime or LocalDate instead of Date.
I don’t want to retain timezone information. I just want the deserialized Date to have exactly the same values as the string, completely ignoring the timezone problem. Can configOverrides be used for deserializion too? Or is it just for serialization?
I’ve seen some testing examples, so I’ve written a few lines in that style:
The resulting date is 1970-01-01 01:00:00 on my system, I expected it to be 1970-01-01 00:00:00.
EDIT: I’ve figured out a workaround, though it is not very nice:
Since I’m using this in spring integration 4.x it requires me to create a modified copy of Jackson2JsonObjectMapper that sets the date format. Also the workaround breaks if the date pattern is set using annotations:
Is there any better way of solving this issue?
Adding to what has been said above: The confusion here might come from the fact the even though
java.util.Datehas no timezone data, the output ofDate.toString()uses your default timezone for displaying purposes (which is then likely also shown while debugging). But this is really only for displaying purposes;Date.getTime()will show you that regardless of your local default timezone the data will be the same. Here is an example:So this is just another reason why, as mentioned above, you should avoid
java.util.Dateand prefer thejava.time.*classes.@kst83 I don’t think you are listening.
java.util.Datedoes not have concept of hours, minutes or any other components. It is a wrapper around 64-bit values. Your IDE may choose to construct aCalendarfrom it to try to be helpful. That does not mean they exist as distinct values without necessary information: specific timepoint IN WHICH TIMEZONE (or, formatted how? Date does not have formatting either).There are many cases where just printing out strings (or looking at Strings your tools show you) does not help unless you are understand what transformations are being done for you, and I find it frustrating to talk when you seem unwilling to learn about these aspects.
@kst83 That is the exact same value: value is 64-bit
long, seegetTime()method and verify values.Datedoes not have specific String representation any more than timezone assignment. One can write textual representation, with formatting and timezone.At this point I think you really need to learn more about Java Date handling – I have given pointers that should help.
@kst83 I am not sure you quite understand my comment: value of
java.util.Dateliterally do not have timezone information – they do not “know” timezone you might want to use. There is absolutely nothing retained even if incoming String did have timezone: this timezone is used for calculations but then dropped. If you want to retain it, you are better off with Joda or Java 8 date/time values which do retain this information.As to configuration, yes, those are the ways to force use of specific timezone for serialization. There is third way via
ObjectMapper.configOverrides(Date.class).setFormat(....), which allows assigning specific formats for all instances ofDate. But I am guessing you do want it applied to all types anyway.