|
package com.example.datetime; |
|
|
|
import com.fasterxml.jackson.annotation.JsonFormat; |
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
|
import org.junit.jupiter.api.Test; |
|
|
|
import java.time.*; |
|
import java.util.Map; |
|
|
|
/** |
|
* Test class demonstrating Jackson JSON serialization of various Java date/time types. |
|
* This test shows how different @JsonFormat annotations affect the serialization output |
|
* for LocalDateTime, OffsetDateTime, ZonedDateTime, and Instant objects. |
|
*/ |
|
public class DateTimeSerializationTest { |
|
|
|
/** |
|
* Sample DTO class containing various date/time fields with different JsonFormat configurations. |
|
* Each field demonstrates a different serialization pattern and timezone handling approach. |
|
*/ |
|
static class SampleDto { |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'") |
|
public LocalDateTime localWithZ; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "UTC") |
|
public LocalDateTime localWithTimezone; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "UTC") |
|
public LocalDateTime localWithHardcodedZ; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "UTC") |
|
public LocalDateTime localWithIsoFormatUtc; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "Asia/Seoul") |
|
public LocalDateTime localWithIsoFormatKst; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX", timezone = "UTC") |
|
public OffsetDateTime offsetDateTime; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssXXX", timezone = "UTC") |
|
public ZonedDateTime zonedDateTime; |
|
|
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'", timezone = "UTC") |
|
public Instant instant; |
|
} |
|
|
|
@Test |
|
public void testDateTimeSerialization() throws Exception { |
|
// Create ObjectMapper with JavaTimeModule for Java 8 time support |
|
ObjectMapper mapper = new ObjectMapper(); |
|
mapper.registerModule(new JavaTimeModule()); |
|
|
|
// Create a specific date/time for consistent test results |
|
LocalDateTime now = LocalDateTime.of(2025, 6, 18, 15, 30, 45); |
|
|
|
// Create and populate the sample DTO |
|
SampleDto dto = new SampleDto(); |
|
dto.localWithZ = now; |
|
dto.localWithTimezone = now; |
|
dto.localWithHardcodedZ = now; |
|
dto.localWithIsoFormatUtc = now; |
|
dto.localWithIsoFormatKst = now; |
|
dto.offsetDateTime = now.atOffset(ZoneOffset.UTC); |
|
dto.zonedDateTime = now.atZone(ZoneId.of("UTC")); |
|
dto.instant = now.atZone(ZoneId.of("UTC")).toInstant(); |
|
|
|
// Serialize the DTO to JSON string |
|
String json = mapper.writeValueAsString(dto); |
|
System.out.println("Serialized JSON:"); |
|
System.out.println(json); |
|
System.out.println(); |
|
|
|
// Parse the JSON back to a Map for detailed field inspection |
|
Map<String, String> result = mapper.readValue(json, Map.class); |
|
|
|
System.out.println("Individual field serialization results:"); |
|
result.forEach((fieldName, serializedValue) -> |
|
System.out.printf("%-25s → %s%n", fieldName, serializedValue)); |
|
|
|
System.out.println(); |
|
System.out.println("Field descriptions:"); |
|
System.out.println("localWithZ → LocalDateTime with hardcoded 'Z' suffix (no timezone conversion)"); |
|
System.out.println("localWithTimezone → LocalDateTime converted to UTC timezone (no Z suffix)"); |
|
System.out.println("localWithHardcodedZ → LocalDateTime converted to UTC with hardcoded 'Z' suffix"); |
|
System.out.println("localWithIsoFormatUtc → LocalDateTime with milliseconds, UTC timezone (shows Z)"); |
|
System.out.println("localWithIsoFormatKst → LocalDateTime with milliseconds, Seoul timezone (converted time)"); |
|
System.out.println("offsetDateTime → OffsetDateTime with timezone offset (shows Z)"); |
|
System.out.println("zonedDateTime → ZonedDateTime converted to UTC with offset (shows Z)"); |
|
System.out.println("instant → Instant (always UTC) with 'Z' suffix"); |
|
|
|
System.out.println(); |
|
System.out.println("Expected output:"); |
|
System.out.println("localWithZ → 2025-06-18T15:30:45Z"); |
|
System.out.println("localWithTimezone → 2025-06-18T15:30:45"); |
|
System.out.println("localWithHardcodedZ → 2025-06-18T15:30:45Z"); |
|
System.out.println("localWithIsoFormatUtc → 2025-06-18T15:30:45.000"); |
|
// Note: localWithIsoFormatKst will be not converted to Seoul time. Becasuse type is LocalDateTime. |
|
System.out.println("localWithIsoFormatKst → 2025-06-18T15:30:45.000"); |
|
System.out.println("offsetDateTime → 2025-06-18T15:30:45Z"); |
|
System.out.println("zonedDateTime → 2025-06-18T15:30:45Z"); |
|
System.out.println("instant → 2025-06-18T15:30:45Z"); |
|
} |
|
} |