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

查看:34
本文介绍了将 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 Bad Request.

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>();
}

推荐答案

使用@InitBinderWebDataBinder:

@RestController
public class SimpleController {

    //... your handlers here...

    @InitBinder
    public void initBinder(final WebDataBinder webdataBinder) {
        webdataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(new Date(Long.valueOf(text)));
            }
        });
    }
}

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

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