Mastering Date Formatting in Spring Boot 3: 5 Practical Solutions

This article explains why proper date formatting matters in Spring Boot applications, compares the default Jackson behavior, and presents five concrete approaches—including global configuration, @JsonFormat, custom ObjectMapper, @JsonComponent, and @JsonSerialize—detailing their implementations, advantages, and drawbacks.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Date Formatting in Spring Boot 3: 5 Practical Solutions

Environment: Spring Boot 3.4.2

Introduction

In project development, date formatting is a core requirement for data interaction. Different scenarios need different formats: the front‑end often wants a readable yyyy‑MM‑dd , databases prefer TIMESTAMP , and cross‑time‑zone systems use ISO‑8601 (e.g., 2025‑01‑01T14:30:00Z). Improper handling can cause time‑zone errors, parsing failures, or data inconsistency. By default, Jackson outputs dates in a format that is usually not desired and uses the UTC (zero) time‑zone, while most Chinese projects need the UTC+8 zone.

1. Global configuration via application.yml or application.properties

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

Pros: Simple and efficient; applies to all date fields across the project without code changes.

Cons: Only supports SimpleDateFormat patterns, cannot set different formats per field, and lacks flexibility for mixed requirements.

2. @JsonFormat annotation on fields

public record User(Long id, String name,
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8") Date birthday,
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") Date createTime) {}

Pros: Precise control of format and time‑zone for each individual field, suitable when different fields require different representations.

Cons: Must annotate every date field; code becomes repetitive and hard‑coded, especially with many date fields.

3. Custom ObjectMapper via Jackson2ObjectMapperBuilderCustomizer

@Component
public class JacksonConfig implements Jackson2ObjectMapperBuilderCustomizer {
    @Override
    public void customize(Jackson2ObjectMapperBuilder builder) {
        builder.simpleDateFormat("yyyy-MM-dd HH:mm").timeZone("GMT+8");
    }
}

Pros: Centralized bean that sets a global default date format for the whole application, easy to maintain for large projects.

Cons: Enforces a single format globally; special cases still need @JsonFormat overrides.

4. Global serializer with @JsonComponent

@JsonComponent
public class GlobalDateSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        gen.writeString(sdf.format(date));
    }
}

Pros: Registers a serializer that automatically formats all Date types, providing a single place to manage the format logic.

Cons: Requires writing an extra class; adds some overhead for simple scenarios.

5. Custom serializer with @JsonSerialize and JsonSerializer

public class DateSerializer extends JsonSerializer<Date> {
    @Override
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-19 HH:mm:ss");
        gen.writeString(sdf.format(date));
    }
}

public record User(Long id, String name, Date birthday,
    @JsonSerialize(using = DateSerializer.class) Date createTime) {}

Pros: Allows highly customized conversion logic, suitable for complex requirements such as converting timestamps to specific strings.

Cons: Increases code volume and maintenance effort; overkill for ordinary use cases.

Additional Note

All the above methods also support Java 8 date‑time types such as LocalDate and LocalDateTime.

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.

javaSpring BootJacksonDate Formatting
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

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.