将Unix时间戳转换为Java日期,Spring RequestParam [英] Convert Unix timestamp to Java Date, Spring RequestParam

查看:140
本文介绍了将Unix时间戳转换为Java日期,Spring RequestParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Following is a request fullcalendar js send to the server.

http://localhost:8080/NVB/rest/calendar/events?start=1425168000&end=1428796800 400

如何在Spring Request Param中指定日期模式( @DateTimeFormat )以将此时间转换为Date对象。我尝试了不同的模式,但收到405错误的请求。

How to specify Date pattern (@DateTimeFormat) in Spring Request Param to convert this time to a Date object. I tried different patterns but getting 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>();
}


推荐答案

因为时间戳不是格式化的日期(通过Java的 SimpleDateFormat 选项进行选择),但更多的是数字值:如果您经常这样做,我建议为Date对象创建一个自定义数据绑定器这个单一实例。参见 http://docs.spring .io / spring / docs / current / spring-framework-reference / htmlsingle /#portlet-ann-webdatabinder

Since timestamps aren't a formatted date (going by Java's SimpleDateFormat options), but more a numeric value: I would recommend making a custom data-binder for Date objects if you're doing this more often than this single instance. See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#portlet-ann-webdatabinder

作为一种一次性解决方案,您可以将它们绑定到 Long 参数,并使用 new Date(start)创建自己的Date对象。

As a one-off solution you can bind them to Long parameters and create your own Date object with new Date(start).

这篇关于将Unix时间戳转换为Java日期,Spring RequestParam的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆