Date format in the json output using spring boot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
By default, Spring Boot's Jackson serializer converts java.util.Date to a Unix timestamp (milliseconds since epoch) and java.time.LocalDateTime to an array like [2025,9,23,14,30,0]. To get human-readable formats like "2025-09-23T14:30:00", configure the date format either globally in application.properties, per-field with @JsonFormat, or via a custom ObjectMapper bean. The recommended approach for new code is to use java.time classes (LocalDate, LocalDateTime, ZonedDateTime) with the jackson-datatype-jsr310 module, which Spring Boot auto-configures.
The Default Problem
Fix 1: application.properties (Global)
write-dates-as-timestamps=false is the key setting. It tells Jackson to serialize dates as ISO-8601 strings instead of numeric timestamps or arrays.
Fix 2: @JsonFormat (Per Field)
@JsonFormat overrides the global configuration for specific fields. Use it when different fields need different formats.
Fix 3: Custom ObjectMapper Bean
This gives full control over serialization. Spring Boot uses this ObjectMapper for all JSON processing.
Fix 4: Custom Serializer
Handling Deserialization (JSON to Java)
The @JsonFormat pattern works for both serialization (Java to JSON) and deserialization (JSON to Java).
Time Zone Handling
Common Pitfalls
- Missing
jackson-datatype-jsr310: Java 8 date/time classes (LocalDate,LocalDateTime) require thejackson-datatype-jsr310module. Spring Boot auto-includes it, but if you customize theObjectMapper, you must registernew JavaTimeModule()manually or these types serialize as arrays. @JsonFormattimezone ignored for LocalDateTime:LocalDateTimehas no timezone concept. Settingtimezonein@JsonFormathas no effect on it. UseZonedDateTimeorOffsetDateTimewhen timezone matters.- Date format not applied to deserialization: If your
@JsonFormatpattern does not match the incoming JSON string exactly, Jackson throwsInvalidFormatException. Ensure the request format matches the pattern precisely. - Global format overridden by
@JsonFormat:@JsonFormaton a field takes precedence overapplication.propertiessettings. If one field shows a different format, check for a field-level annotation. - Using
java.util.Datein new code:java.util.Dateis mutable, not timezone-aware, and cumbersome to format. Usejava.time.LocalDateTimeorjava.time.ZonedDateTimefor new code — they work better with Jackson and are immutable.
Summary
- Set
spring.jackson.serialization.write-dates-as-timestamps=falseto get ISO-8601 strings instead of timestamps - Use
@JsonFormat(pattern = "...")for per-field format control - Spring Boot auto-registers
JavaTimeModule— usejava.timeclasses (LocalDate,LocalDateTime,ZonedDateTime) - Configure global format in
application.propertiesor via a customObjectMapperbean - Use
ZonedDateTimeorOffsetDateTimewhen timezone information is needed @JsonFormatworks for both serialization (output) and deserialization (input)
Related reading
- Dead letter queue (DLQ) for Kafka with spring-kafka
- Dealing with "java.lang.OutOfMemoryError PermGen space" error
- Dealing with Xerces hell in Java/Maven?
- Debugging multiple threads in eclipse
- Decentralized Clustering library for Java
- DecimalFormat.formatdouble in different threads
- Decision tree implementation issue in apache spark with java
- Declaring an unsigned int in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.