字符串转换日期:
1.自定义一个类
/**
* 字符串转换日期
*/
public class StringToDateConverter implements Converter<String, Date> { /**
* String source 传入进来字符串
* @param source
* @return
*/
@Override
public Date convert(String source) {
//判断
if(source == null){
//抛出运行时异常
throw new RuntimeException("请您传入数据");
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
//把字符串转换日期
return df.parse(source);
} catch (ParseException e) {
throw new RuntimeException("数据类型转换出现错误");
} }
}
二、在springmvc.xml中配置自定义类型转换器
<!--配置自定义类型转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="cn.flypig666.utils.StringToDateConverter"></bean>
</set>
</property>
</bean> <!--开启SpringMVC框架注解的支持-->
<mvc:annotation-driven conversion-service="conversionService"/>