88import java .io .IOException ;
99import java .text .ParseException ;
1010import java .text .SimpleDateFormat ;
11+ import java .time .LocalDate ;
1112import java .time .LocalDateTime ;
1213import java .time .ZoneId ;
1314import java .time .ZonedDateTime ;
1819import org .joda .time .DateTimeZone ;
1920import org .junit .Test ;
2021
21- import com .baeldung .jackson .date .Event ;
22- import com .baeldung .jackson .date .EventWithFormat ;
23- import com .baeldung .jackson .date .EventWithJodaTime ;
24- import com .baeldung .jackson .date .EventWithLocalDateTime ;
25- import com .baeldung .jackson .date .EventWithSerializer ;
2622import com .fasterxml .jackson .core .JsonProcessingException ;
2723import com .fasterxml .jackson .databind .ObjectMapper ;
2824import com .fasterxml .jackson .databind .SerializationFeature ;
@@ -57,7 +53,7 @@ public void whenSerializingDateToISO8601_thenSerializedToText() throws JsonProce
5753
5854 final ObjectMapper mapper = new ObjectMapper ();
5955 mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
60-
56+
6157 // StdDateFormat is ISO8601 since jackson 2.9
6258 mapper .setDateFormat (new StdDateFormat ().withColonInTimeZone (true ));
6359
@@ -143,7 +139,7 @@ public void whenSerializingJava8DateWithCustomSerializer_thenCorrect() throws Js
143139 }
144140
145141 @ Test
146- public void whenDeserializingDateWithJackson_thenCorrect () throws JsonProcessingException , IOException {
142+ public void whenDeserializingDateWithJackson_thenCorrect () throws IOException {
147143 final String json = "{\" name\" :\" party\" ,\" eventDate\" :\" 20-12-2014 02:30:00\" }" ;
148144
149145 final SimpleDateFormat df = new SimpleDateFormat ("dd-MM-yyyy hh:mm:ss" );
@@ -156,7 +152,7 @@ public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessing
156152 }
157153
158154 @ Test
159- public void whenDeserializingDateUsingCustomDeserializer_thenCorrect () throws JsonProcessingException , IOException {
155+ public void whenDeserializingDateUsingCustomDeserializer_thenCorrect () throws IOException {
160156 final String json = "{\" name\" :\" party\" ,\" eventDate\" :\" 20-12-2014 02:30:00\" }" ;
161157
162158 final SimpleDateFormat df = new SimpleDateFormat ("dd-MM-yyyy hh:mm:ss" );
@@ -179,6 +175,28 @@ public void whenSerializingJava8Date_thenCorrect() throws JsonProcessingExceptio
179175 assertThat (result , containsString ("2014-12-20T02:30" ));
180176 }
181177
178+ @ Test
179+ public void whenSerializingJava8DateAndReadingValue_thenCorrect () throws IOException {
180+ String stringDate = "\" 2014-12-20\" " ;
181+
182+ ObjectMapper mapper = new ObjectMapper ();
183+ mapper .registerModule (new JavaTimeModule ());
184+ mapper .disable (SerializationFeature .WRITE_DATES_AS_TIMESTAMPS );
185+
186+ LocalDate result = mapper .readValue (stringDate , LocalDate .class );
187+ assertThat (result .toString (), containsString ("2014-12-20" ));
188+ }
189+
190+ @ Test
191+ public void whenSerializingJava8DateAndReadingFromEntity_thenCorrect () throws IOException {
192+ String json = "{\" name\" :\" party\" ,\" eventDate\" :\" 20-12-2014\" }" ;
193+
194+ ObjectMapper mapper = new ObjectMapper ();
195+
196+ EventWithLocalDate result = mapper .readValue (json , EventWithLocalDate .class );
197+ assertThat (result .getEventDate ().toString (), containsString ("2014-12-20" ));
198+ }
199+
182200 @ Test
183201 public void whenSerializingJodaTime_thenCorrect () throws JsonProcessingException {
184202 final DateTime date = new DateTime (2014 , 12 , 20 , 2 , 30 , DateTimeZone .forID ("Europe/London" ));
0 commit comments