首页 > 编程语言 > 详细

springmvc的日期类型转换

时间:2019-03-25 20:06:08      阅读:166      评论:0      收藏:0      [点我收藏+]

            springmvc的日期类型转换

# spring mvc绑定参数之类型转换有三种方式:

## 1.实体类中加日期格式化注解

@DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
private Date creationTime;

## 2.属性编辑器

spring3.1之前

在Controller类中通过@InitBinder完成

/**
* 在controller层中加入一段数据绑定代码
* @param webDataBinder
*/
@InitBinder
public void initBinder(WebDataBinder webDataBinder) throws Exception{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
simpleDateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class , new CustomDateEditor(simpleDateFormat , true));
}
备注:自定义类型转换器必须实现PropertyEditor接口或者继承PropertyEditorSupport类
写一个类 extends propertyEditorSupport(implements PropertyEditor){
public void setAsText(String text){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy -MM-dd hh:mm");
Date date = simpleDateFormat.parse(text);
this.setValue(date);
}
public String getAsTest(){
Date date = (Date)this.getValue();
return this.dateFormat.format(date);
}
}

## 3. 类型转换器Converter

全局类型转换

2019-03-2519:52:18

springmvc的日期类型转换

原文:https://www.cnblogs.com/itboxue/p/10596043.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!