1717import org .baeldung .jackson .date .EventWithLocalDateTime ;
1818import org .baeldung .jackson .date .EventWithSerializer ;
1919import org .joda .time .DateTime ;
20+ import org .joda .time .DateTimeZone ;
2021import org .junit .Test ;
2122
2223import com .fasterxml .jackson .core .JsonProcessingException ;
2324import com .fasterxml .jackson .databind .ObjectMapper ;
2425import com .fasterxml .jackson .databind .SerializationFeature ;
26+ import com .fasterxml .jackson .datatype .joda .JodaModule ;
27+ import com .fasterxml .jackson .datatype .jsr310 .JSR310Module ;
2528
2629public class JacksonDateTest {
2730
@@ -140,4 +143,28 @@ public void whenDeserializeDateUsingCustomDeserializer_thenCorrect() throws Json
140143 assertEquals ("20-12-2014 02:30:00" , df .format (event .eventDate ));
141144 }
142145
143- }
146+ @ Test
147+ public void whenSerializeJava8Date_thenCorrect () throws JsonProcessingException {
148+ final LocalDateTime date = LocalDateTime .of (2014 , 12 , 20 , 2 , 30 );
149+
150+ final ObjectMapper mapper = new ObjectMapper ();
151+ mapper .registerModule (new JSR310Module ());
152+ mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
153+
154+ final String result = mapper .writeValueAsString (date );
155+ assertThat (result , containsString ("2014-12-20T02:30" ));
156+ }
157+
158+ @ Test
159+ public void whenSerializeJodaTime_thenCorrect () throws JsonProcessingException {
160+ final DateTime date = new DateTime (2014 , 12 , 20 , 2 , 30 , DateTimeZone .forID ("Europe/London" ));
161+
162+ final ObjectMapper mapper = new ObjectMapper ();
163+ mapper .registerModule (new JodaModule ());
164+ mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
165+
166+ final String result = mapper .writeValueAsString (date );
167+ assertThat (result , containsString ("2014-12-20T02:30:00.000Z" ));
168+ }
169+
170+ }
0 commit comments