How to make default time zone apply in Spring Boot Jackson Date serialization
Asked Answered
B

4

30

I have configured my Spring Boot application to serialize dates as ISO8601 strings:

spring:
  jackson:
    serialization:
      write-dates-as-timestamps: false

This is what I am getting:

"someDate": "2017-09-11T07:53:27.000+0000"

However my time zone is Europe/Madrid. In fact if I print TimeZone.getDefault() that's what I get.

How can I make Jackson serialize those datetime values using the actual timezone? GMT+2

"someDate": "2017-09-11T09:53:27.000+0200"
Bluebeard answered 11/9, 2017 at 8:50 Comment(0)
B
38

Solved registering a Jackson2ObjectMapperBuilderCustomizer bean:

@Bean
public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
    return jacksonObjectMapperBuilder -> 
        jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
}
Bluebeard answered 11/9, 2017 at 9:5 Comment(1)
Hello, after adding this config and use @JsonFormat annotation with specific pattern it does not seem to find the default timezone: @JsonFormat(pattern = "yyyy-MM-dd")Lilt
T
38

I found myself with the same problem. In my case, I have only one timezone for my app, then adding:

spring.jackson.time-zone: America/Sao_Paulo

in my application.properties solved the problem.

Source: https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html#JACKSON

Tarsia answered 21/12, 2017 at 18:42 Comment(0)
W
33

You can set timezone for whole application with adding this to a config class:

@PostConstruct
void started() {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
Whist answered 11/9, 2017 at 12:8 Comment(5)
Jackson won't pick that up. The default TimeZone is the right one. My solution solves it. Thanks anywayBluebeard
@Bluebeard I use jackson in my project too and this configuration solved my problem for time zones. Anyway if you solve your problem, nothing to argue :) I will just leave my post if anybody can use it. Happy coding.Whist
yes this will work, it depends where in your application you are setting default time zone. this work's good because now ur application will not do any time adjustment.Twentyfour
This sets the "target" timezone of (de)serialization, while the JsonFormat(timezone = "xxx") sets the input timezone of (de)serialization when parsing as well as formating. See another question of mine for details.Vlada
@Whist solution solved my problem. In my case I save OffsetDateTimes on database as offset "+00:00" (or "Z"), and when retrieving them, the applicaiton automatically added the offset. With this setting it doesn't. Thanks! :)Toy
G
6

There are 2 solutions for this:

1. Add JSON format annotation

@JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "Asia/Kolkata")
private Date insertionTime;

2. Add Jackson time zone to properties (spring boot)

spring.jackson.time-zone: America/Sao_Paulo

Reference : https://www.baeldung.com/spring-boot-formatting-json-dates

Grimmett answered 5/12, 2019 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.