- 定义转换器
package com.company.combine.conveter;
import org.springframework.core.convert.converter.Converter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by liuqun on 2017/8/16.
*/
public class DateConverter implements Converter<String,Date> {
@Override
public Date convert(String s) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
Date date = null;
try {
date = sdf.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
- Spring中配置
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean id="dateConverter" class="com.company.combine.conveter.DateConverter" />
</set>
</property>
</bean>
3.测试
@RequestMapping(value = "/updateitem.action")
public void updateItem(Date createtime) {
System.out.println("createtime:" + createtime.toString());
}
4.输入
2017-08-18 22-10-10
5.结果
createtime:Fri Aug 18 22:10:10 CST 2017