Binder:
这种类型转换的时候,使用的是Spring的参数绑定,代码如下:
/**
* Description: 全局类型转换
* Author: liuzh
* Update: liuzh(2014-05-26 13:08)
*/
public class GlobalDataBinder implements WebBindingInitializer {
/**
* 智能日期转换,针对四种格式日期:
* 1.2014-05-26
* 2.1401951570548
* 3.2014-05-26 00:00
* 4.2014-05-26 00:00:00
*/
private class SmartDateEditor extends PropertyEditorSupport {
/**
* 根据2014-05-26 00:00:00长度来判断选择哪种转换方式
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null || text.length() == 0) {
setValue(null);
} else {
try {
if (text.length() == 10) {
setValue(DateUtil.formatStringToDate(text, DateUtil.DATE_FORMAT_YYYYMMDD));
} else if (text.length() == 13) {
setValue(new Date(Long.parseLong(text)));
} else if (text.length() == 16) {
setValue(DateUtil.formatStringToDate(text, DateUtil.DATE_FORMAT_TIME_R));
} else if (text.length() == 19) {
setValue(DateUtil.formatStringToDate(text, DateUtil.DATE_FORMAT_TIME_T));
} else {
throw new IllegalArgumentException("转换日期失败: 日期长度不符合要求!");
}
} catch (Exception ex) {
throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex);
}
}
}
/**
* 转换为日期字符串
*/
@Override
public String getAsText() {
Date value = (Date) getValue();
String dateStr = null;
if (value == null) {
return "";
} else {
try {
dateStr = DateUtil.formatDate(value, DateUtil.DATE_FORMAT_TIME_T);
if (dateStr.endsWith(" 00:00:00")) {
dateStr = dateStr.substring(0, 10);
} else if (dateStr.endsWith(":00")) {
dateStr = dateStr.substring(0, 16);
}
return dateStr;
} catch (Exception ex) {
throw new IllegalArgumentException("转换日期失败: " + ex.getMessage(), ex);
}
}
}
}
@Override
public void initBinder(WebDataBinder binder, WebRequest request) {
//日期格式转换
binder.registerCustomEditor(Date.class, new SmartDateEditor());
}
}
这里对日期类型进行了一些判断来特殊处理。该类需要在Spring的xml进行配置:
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="com.easternie.sys.common.GlobalDataBinder"/>
</property>
</bean>
通过这种配置之后,Spring就能对日期进行智能转换了。
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/102727.htm
