我想将所有java8 ZonedDateTimes转换为应用程序服务器端的UTC时区。我使用@DateTimeFormat成功地将Java 8 jsr310日期数据类型绑定到spring RestController中。
@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
)
对于ZonedDateTime类,我想将所有输入ZonedDateTimes转换为UTC时间,因此服务器端始终以UTC时区工作。遵循最佳实践#3 - 将其存储在UTC中 http://apiux.com/2013/03/20/5-laws-api-dates-and-times/#comments
对于JSON反序列化,我有一个ZonedDateTime的自定义反序列化器,可以将任何时区转换为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"));
在控制器绑定中进行转换的最佳方法是什么?我理解这可能会迫使多个职责进入同一个班级,但我想避免添加
ZonedDateTime.withZoneSameInstant
呼叫每个控制器中的每个日期 感谢