Convert Unix timestamp to Java Date, Spring RequestParam

≡放荡痞女 提交于 2019-11-28 12:21:50

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

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

Using @InitBinder and WebDataBinder:

@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)));
            }
        });
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!