Spring mvc Convert ZonedDateTime to UTC

I woudl ike to convert all java8 ZonedDateTimes to UTC time on the server side of the application. I successfully bind Java 8 jsr310 date data types to

@RequestMapping(value = "rest/test-date", method = RequestMethod.GET) public TestCollection findPrivilegesByRoleList( @RequestParam(value = "local-date", defaultValue = "2015-05-10") @DateTimeFormat(iso = ISO.DATE) LocalDate requestParamDate, @RequestParam(value = "local-date-time", defaultValue = "2015-05-16T15:55:56") @DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime requestParamLocalDateTime, @RequestParam(value = "zoned-date-time", defaultValue = "2015-05-18T11:55:56-04:00") @DateTimeFormat(iso = ISO.DATE_TIME) ZonedDateTime requestParamZonedDateTime ) 

For the ZonedDateTime class, I would like to switch all ZonedDateTimes input to UTC, so the server side always works in the UTC time zone. Following best practice number 3 - Save it at UTC http://apiux.com/2013/03/20/5-laws-api-dates-and-times/#comments

For JSON deserialization, I have a custom deserializer for ZonedDateTime that shifts any time zone in UTC.

 .... //Parse string into a zoned date time with the specified timezone offset - EST, CET, EDT, PST, ect. ZonedDateTime zonedDateTimewithTimeZone = ZonedDateTime.parse(string, formatter); //Shift the date time to UTC Time return zonedDateTimewithTimeZone.withZoneSameInstant(ZoneId.of("UTC")); 

What is the best way to do the conversion binding to the controller? I understand that this can lead to multiple responsibilities in one class, however I want to avoid adding

ZonedDateTime.withZoneSameInstant

call each date on each controller.
Thanks

+5
source share

All Articles