Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot, define timezone for Json and java.time.* globally

I'm using Spring Boot 1.5.3, Spring Data REST, HATEOAS to create a REST service. I use java.time.* date/times in my application and I'm storing that in UTC format in the database. I want to follow best practice and return UTC dates in my REST endpoints.

Using @JsonFormat I'm able to accomplish to that:

@JsonFormat(timezone = "UTC", pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
@LastModifiedDate
private LocalDateTime lastModifiedDate;

I would like to avoid to annotate all my beans with that, and I would prefer to have a global configuration. According to this enhancemente request, I was able to solve 50% of my problem with this configuration:

@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    return new Jackson2ObjectMapperBuilderCustomizer() {

        @Override
        public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
            jacksonObjectMapperBuilder.serializers(
                    new LocalDateTimeSerializer(new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd'T'HH:mm:ss").toFormatter()));
            jacksonObjectMapperBuilder.serializers(new ZonedDateTimeSerializer(
                    new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").toFormatter()));
        }

    };
}

This fix the pattern problem but not the timezone problem. My timezone continue to remain the local timezone. Is there a graceful way to set the timezone of JSON serializer to UTC like the property spring.jackson.time-zone=UTC that unfortunately work just for java.util.time?

like image 591
drenda Avatar asked Jul 03 '17 06:07

drenda


People also ask

How do I set timezone in JDBC?

To set the timezone for a given JDBC connection, navigate to the Advanced tab and select the timezone from the dropdown menu. By default, UTC is selected.

Is Instant always UTC?

For example, 2022-06-29T12:01:25.369081700Z. Note:- Instant date/time are time-zone unaware and it always returns current date/time at UTC/GMT.

What is UTC timezone in Java?

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.


1 Answers

I think you need to set the timezone of your formatter.

See https://github.com/jhipster/generator-jhipster/blob/09f0957b3ac9711fb9dd86f77ad69c549401074f/generators/server/templates/src/main/java/package/config/_JacksonConfiguration.java

@Configuration
public class JacksonConfiguration {

    public static final DateTimeFormatter ISO_FIXED_FORMAT =
        DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneId.of("Z"));

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jacksonCustomizer() {
        return new Jackson2ObjectMapperBuilderCustomizer() {
            @Override
            public void customize(Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) {
                jackson2ObjectMapperBuilder.serializers(new ZonedDateTimeSerializer(ISO_FIXED_FORMAT));
            }
        };
    }
}
like image 56
Christophe Bornet Avatar answered Nov 15 '22 10:11

Christophe Bornet