0

I've a Spring Boot application with an API. Some of the endpoints returns responses with data serialised from OffsetDateTime. Some of these dates comes from the database and others are generated at runtime (OffsetDateTime.now()), which cause the OffsetDateTime to have different timezones. I like to keep using the OffsetDateTime on my API to respons the time zone, but I would like all dates serialised from my API to be in the same timezone -- in my case UTC. How can I globally configure Jackson to do that?

dhrm
  • 14,335
  • 34
  • 117
  • 183

1 Answers1

0

Spring Boot Property:

spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jackson.time-zone=UTC

JVM Default Timezone:

@PostConstruct
void started() {
  TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
Marcus Voltolim
  • 413
  • 4
  • 12
  • The `setDefault` make my OffsetDateTimes created using `OffsetDateTime.now()` being in UTC, which makes them UTC when serialized using Jackson. That's good, but if I somehow creates an OffsetDateTime in another time zone, it's still returned in that timezone. The Spring Boot properties does not seem to work properly. – dhrm Sep 25 '20 at 07:58
  • @dhrm In that case you will need to manually convert to UTC. Or create a custom serializer and add it globally, similar to this: https://stackoverflow.com/a/40051029/4279074 – Marcus Voltolim Nov 07 '22 at 04:40