将Unix时间戳转换为Java Date,Spring RequestParam

Sus*_*ath 6 datetime spring-mvc unix-timestamp

以下是请求fullcalendar js发送到服务器.

http://localhost:8080/NVB/rest/calendar/events?start=1425168000&end=1428796800 400
Run Code Online (Sandbox Code Playgroud)

如何@DateTimeFormat在Spring Request Param中指定Date pattern()以将此时间转换为Date对象.我尝试了不同的模式,但得到了405 Bad Request.

@RequestMapping(value = "/events", method = RequestMethod.GET)
public @ResponseBody List<EventDto> addOrder(@RequestParam(value = "start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date start,
                                             @RequestParam(value = "end") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)   Date end) {
    LOGGER.info("Requesting event from [{}] to [{}]", start, end);
    return new LinkedList<EventDto>();
}
Run Code Online (Sandbox Code Playgroud)

Kaf*_*que 3

由于时间戳不是格式化日期(按照 Java 的SimpleDateFormat选项),而是一个数值:如果您比单个实例更频繁地执行此操作,我建议您为 Date 对象创建自定义数据绑定器。请参阅http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#portlet-ann-webdatabinder

作为一次性解决方案,您可以将它们绑定到Long参数并使用new Date(start).