Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

Serialization and deserialization of LocalDateTime and LocalDate are different in Java 8

global

Configure JavaTimeModule in the ObjectMapper object. This is a global configuration.

    @Bean
    public ObjectMapper objectMapper(a) {
        ObjectMapper objectMapper = new ObjectMapper();
        
        // other serializer and deSerializer config ...
        
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }
Copy the code

DateTimeFormatter. OfPattern can set different time date templates, to achieve different effects

local

use@JsonFormatannotations

Pattern allows you to configure different time format templates

@Data
public static class Article {
    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDateTime date;
}
Copy the code

The Serializer and DeSerializer

Jackson provides Serializer and DeSerializer for LocalDate and LocalDateTime by default, but requires the introduction of additional Maven dependencies

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-joda</artifactId>
    <version>2.9.5</version>
</dependency>
Copy the code
@Data
public static class Article {
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonDeserialize(using = LocalDateDeserializer.class)
    private LocalDateTime date;
}
Copy the code

In addition, you can also customize Serializer and DeSerializer to meet specific time and date formats. For example, deserialize any format of the same time into a standard LocalDateTime object.

public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        return this.deserialize(p.getText().trim());
    }

    private LocalDateTime deserialize(String source) {
        if (StringUtils.isBlank(source)) {
            return null;
        }  else if (source.matches("^ \ \ d {4} - \ \ d {1, 2} $")) {
            // yyyy-MM
            return LocalDateTime.parse(source + "000-01 t00:00:00.", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        } else if (source.matches("^ \ \ d {4} \ \ d {1, 2} - \ \ d {1, 2} {1} T \ \ d {1, 2}")) {
            // yyyy-MM-ddTHH
            return LocalDateTime.parse(source + ": 00:00. 000", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        }   else {
            // yyyy-MM-ddTHH:mm:ss.SSS
            returnLocalDateTime.parse(source, DateTimeFormatter.ISO_LOCAL_DATE_TIME); }}}Copy the code