springmvc自动转前台传来的时间数据

来源:互联网 发布:js 所有input不可编辑 编辑:程序博客网 时间:2024/06/07 01:57

Spring中默认支持的日期数据的格式是”yyyy-MM-dd”,假设现在需要支持”yyyy-MM-dd HH:mm:ss”格式的日期数据,我们就需要配置WebDataBinder以增加自定义数据绑定方式。

@InitBinderpublic void initBinder(WebDataBinder binder) {    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    binder.registerCustomEditor(Timestamp.class, new CustomDateEditor(dateFormat, false));}

我现在需要将前台传来的时间戳字符串(如:1457263453000)转成Timestamp类型,刚可以按如下方法进行转换
@InitBinderprotected void initBinder(WebDataBinder binder) {binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() {@Overridepublic void setAsText(String value) {setValue(new Timestamp(Long.valueOf(value)));}});}
只需要将上述方法定义成一个父类,在需要的controller中继承一下即可,本人是用springmvc4来实现的

0 0
原创粉丝点击