diff --git a/jackson-modules/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithLocalDate.java b/jackson-modules/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithLocalDate.java new file mode 100644 index 0000000000..c67943f418 --- /dev/null +++ b/jackson-modules/jackson-conversions/src/main/java/com/baeldung/jackson/date/EventWithLocalDate.java @@ -0,0 +1,33 @@ +package com.baeldung.jackson.date; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; + +import java.time.LocalDate; + +public class EventWithLocalDate { + public String name; + + @JsonDeserialize(using = LocalDateDeserializer.class) + @JsonSerialize(using = LocalDateSerializer.class) + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy") + public LocalDate eventDate; + + public EventWithLocalDate() {} + + public EventWithLocalDate(final String name, final LocalDate eventDate) { + this.name = name; + this.eventDate = eventDate; + } + + public LocalDate getEventDate() { + return eventDate; + } + + public String getName() { + return name; + } +} diff --git a/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java b/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java index 924ec1162f..047d53ab62 100644 --- a/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java +++ b/jackson-modules/jackson-conversions/src/test/java/com/baeldung/jackson/date/JacksonDateUnitTest.java @@ -8,6 +8,7 @@ import static org.junit.Assert.assertThat; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -18,11 +19,6 @@ import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.junit.Test; -import com.baeldung.jackson.date.Event; -import com.baeldung.jackson.date.EventWithFormat; -import com.baeldung.jackson.date.EventWithJodaTime; -import com.baeldung.jackson.date.EventWithLocalDateTime; -import com.baeldung.jackson.date.EventWithSerializer; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -57,7 +53,7 @@ public class JacksonDateUnitTest { final ObjectMapper mapper = new ObjectMapper(); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); - + // StdDateFormat is ISO8601 since jackson 2.9 mapper.setDateFormat(new StdDateFormat().withColonInTimeZone(true)); @@ -143,7 +139,7 @@ public class JacksonDateUnitTest { } @Test - public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessingException, IOException { + public void whenDeserializingDateWithJackson_thenCorrect() throws IOException { final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}"; final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); @@ -156,7 +152,7 @@ public class JacksonDateUnitTest { } @Test - public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws JsonProcessingException, IOException { + public void whenDeserializingDateUsingCustomDeserializer_thenCorrect() throws IOException { final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}"; final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); @@ -179,6 +175,28 @@ public class JacksonDateUnitTest { assertThat(result, containsString("2014-12-20T02:30")); } + @Test + public void whenSerializingJava8DateAndReadingValue_thenCorrect() throws IOException { + String stringDate = "\"2014-12-20\""; + + ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JavaTimeModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + + LocalDate result = mapper.readValue(stringDate, LocalDate.class); + assertThat(result.toString(), containsString("2014-12-20")); + } + + @Test + public void whenSerializingJava8DateAndReadingFromEntity_thenCorrect() throws IOException { + String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014\"}"; + + ObjectMapper mapper = new ObjectMapper(); + + EventWithLocalDate result = mapper.readValue(json, EventWithLocalDate.class); + assertThat(result.getEventDate().toString(), containsString("2014-12-20")); + } + @Test public void whenSerializingJodaTime_thenCorrect() throws JsonProcessingException { final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London"));