springmvc自动转前台传来的时间数据
2016-05-18 17:23
363 查看
Spring中默认支持的日期数据的格式是”yyyy-MM-dd”,假设现在需要支持”yyyy-MM-dd HH:mm:ss”格式的日期数据,我们就需要配置WebDataBinder以增加自定义数据绑定方式。
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Timestamp.class, new CustomDateEditor(dateFormat, false));
}
我现在需要将前台传来的时间戳字符串(如:1457263453000)转成Timestamp类型,刚可以按如下方法进行转换
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
binder.registerCustomEditor(Timestamp.class, new CustomDateEditor(dateFormat, false));
}
我现在需要将前台传来的时间戳字符串(如:1457263453000)转成Timestamp类型,刚可以按如下方法进行转换
@InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Timestamp.class, new PropertyEditorSupport() { @Override public void setAsText(String value) { setValue(new Timestamp(Long.valueOf(value))); } }); }只需要将上述方法定义成一个父类,在需要的controller中继承一下即可,本人是用springmvc4来实现的
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- oracle SCN跟TIMESTAMP之间转换
- C#更新SQLServer中TimeStamp字段(时间戳)的方法
- MySQL 5.6 中 TIMESTAMP有那些变化
- MySQL 5.6 中TIMESTAMP with implicit DEFAULT value is deprecated错误
- mysql之TIMESTAMP(时间戳)用法详解
- MySQL timestamp自动更新时间分享
- 模拟Spring的简单实现
- Spring整合WebSocket应用示例(上)
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- springmvc 发送ajax出现中文乱码的解决方法汇总
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的