I have a frontend part in Angular 7 and a backend part in Java with Spring Boot framework. I want to post to my backend a Date object. In backend I have a Local Date object. I don't need LocalDateTime object
my date service in angular. I need to preserve Date type and not use string.
addDate(): Observable<Date> { let now = new Date(); return this.http .post<Date>('/api/date', now) .pipe( tap(response => { return response; }), catchError(error => this.notificationService.handleError(error)) ); } my backend service :
@PostMapping public LocalDate addIrregularity(@RequestBody LocalDate date, HttpServletRequest request) { log.info(date); return date; } And i have this error:
2019-08-06 08:21:02.185 WARN 1444 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type
java.time.LocalDatefrom String "2019-08-06T00:00:00.000+0000": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2019-08-06T00:00:00.000+0000' could not be parsed, unparsed text found at index 23; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of typejava.time.LocalDatefrom String "2019-08-06T00:00:00.000+0000": Failed to deserialize java.time.LocalDate: (java.time.format.DateTimeParseException) Text '2019-08-06T00:00:00.000+0000' could not be parsed, unparsed text found at index 23
LocalDatejust date.LocalDateTimeor maybe evenOffsetDateTimeshould be used here...+0000, is. Just an uninformed guess, it may be that parsing expected the offset to be written with a colon between hours and minutes,+00:00.@RequestBodytoLocalDateTimeand doreturn date.toLocalDate():2019-08-06T00:00:00.000+0000includes an offset-from-UTC on the end. So your input maps tojava.time.OffsetDateTimein Java, notLocalDateTime. For any given moment, the date varies around the globe by time zone, “tomorrow” in Tokyo Japan while simultaneously “yesterday” in Toledo Ohio US. So, apply your desired time zone (ZoneId) to produce aZonedDateTimeobject. From that object you can extract the date (LocalDate).