Reputation: 24492
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"
Upvotes: 30
Views: 73316
Reputation: 1623
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
Upvotes: 6
Reputation: 2379
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.
Upvotes: 38
Reputation: 3154
You can set timezone for whole application with adding this to a config class:
@PostConstruct
void started() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
Upvotes: 33
Reputation: 24492
Solved registering a Jackson2ObjectMapperBuilderCustomizer bean:
@Bean
public Jackson2ObjectMapperBuilderCustomizer jacksonObjectMapperCustomization() {
return jacksonObjectMapperBuilder ->
jacksonObjectMapperBuilder.timeZone(TimeZone.getDefault());
}
Upvotes: 38