diff --git a/jackson/pom.xml b/jackson/pom.xml index 247bab72b0..6196f0e0c6 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -44,6 +44,18 @@ jackson-databind ${jackson.version} + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + 2.4.0 + + + + com.fasterxml.jackson.datatype + jackson-datatype-joda + 2.4.0 + joda-time diff --git a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java b/jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java index 42980baa18..f19de4d42f 100644 --- a/jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java +++ b/jackson/src/test/java/org/baeldung/jackson/test/JacksonDateTest.java @@ -17,11 +17,14 @@ import org.baeldung.jackson.date.EventWithJodaTime; import org.baeldung.jackson.date.EventWithLocalDateTime; import org.baeldung.jackson.date.EventWithSerializer; import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; import org.junit.Test; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.datatype.joda.JodaModule; +import com.fasterxml.jackson.datatype.jsr310.JSR310Module; public class JacksonDateTest { @@ -140,4 +143,28 @@ public class JacksonDateTest { assertEquals("20-12-2014 02:30:00", df.format(event.eventDate)); } -} + @Test + public void whenSerializeJava8Date_thenCorrect() throws JsonProcessingException { + final LocalDateTime date = LocalDateTime.of(2014, 12, 20, 2, 30); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JSR310Module()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + + final String result = mapper.writeValueAsString(date); + assertThat(result, containsString("2014-12-20T02:30")); + } + + @Test + public void whenSerializeJodaTime_thenCorrect() throws JsonProcessingException { + final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London")); + + final ObjectMapper mapper = new ObjectMapper(); + mapper.registerModule(new JodaModule()); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + + final String result = mapper.writeValueAsString(date); + assertThat(result, containsString("2014-12-20T02:30:00.000Z")); + } + +} \ No newline at end of file