json传递Date类型时间出错

来源:互联网 发布:淘宝差评在哪里写 编辑:程序博客网 时间:2024/06/05 12:47
做项目的时候,从前端传递时间参数到spring的controller,结果得到对象的时间结果不对(因为前端接收的格式是Date类型的而非String类型,错误如下图所示:

这里写图片描述
而其实完整的要传递的时间是:2015-07-31 15:07:48

解决办法是在controller加上如下代码:
 @InitBinder    protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        CustomDateEditor dateEditor = new CustomDateEditor(fmt, true);        binder.registerCustomEditor(Date.class, dateEditor);    }

转载请注明原址:http://blog.csdn.net/zheng911209/article/details/48179907

0 0