Mastering Uniform Date Formatting in Spring Boot with Jackson

This guide shows how to configure Jackson in Spring Boot to consistently format LocalDateTime, Date, and String parameters as "yyyy-MM-dd HH:mm:ss", covering global settings via application properties, field-level @JsonFormat, custom serializers/deserializers, and unified handling in REST controllers.

Lin is Dream
Lin is Dream
Lin is Dream
Mastering Uniform Date Formatting in Spring Boot with Jackson

When developing Spring Boot web controllers, you often need to present different time types (LocalDateTime, Date, String) in a unified JSON format. By configuring Jackson, you can standardize the output to the pattern "yyyy-MM-dd HH:mm:ss".

1. Configure Jackson date format

Spring Boot uses Jackson for JSON serialization. You can set the global date format in the configuration file.

spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8  # configure timezone if needed

Or using YAML:

spring:
  jackson:
    date-format: "yyyy-MM-dd HH:mm:ss"
    time-zone: "GMT+8"

This configuration applies to all fields of type Date and LocalDateTime, and Jackson will automatically serialize and deserialize them according to the specified pattern.

2. Use @JsonFormat annotation (field‑level control)

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.Date;

public class MyEntity {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime dateTimeField;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date dateField;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private String dateString;

    // getters and setters ...
}

3. Custom JsonSerializer and JsonDeserializer

If you need more complex handling (e.g., converting a String to LocalDateTime or Date with a special format), you can create custom serializers and deserializers.

Deserializer

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        String dateStr = p.getText();
        return LocalDateTime.parse(dateStr, formatter);
    }
}

Serializer

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeString(value.format(formatter));
    }
}

Register the custom (de)serializers in a Spring configuration:

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfig {
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        objectMapper.registerModule(module);
        return objectMapper;
    }
}

4. Unified handling in a controller

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @PostMapping("/submit")
    public String submit(@RequestBody MyEntity entity) {
        // Spring automatically converts JSON date strings to LocalDateTime or Date
        return "Received: " + entity.getDateTimeField();
    }
}

By configuring Jackson’s date format, you can uniformly process input and output of LocalDateTime, Date, and String types in Spring Boot applications, eliminating the need for manual conversions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaJSONSpring BootjacksonDate Formatting
Lin is Dream
Written by

Lin is Dream

Sharing Java developer knowledge, practical articles, and continuous insights into computer engineering.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.