一、创建自定义日期转换器类
1 public class StringToDateConverter implements Converter<String,Date> { 2 @Override 3 public Date convert(String source) { 4 try { 5 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 6 Date date = simpleDateFormat.parse(source); 7 return date; 8 } catch (ParseException e) { 9 e.printStackTrace(); 10 } 11 return null; 12 } 13 }
二、在springmvc配置文件中设置
1 <mvc:annotation-driven conversion-service="conversionService"/> 2 <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> 3 <property name="converters"> 4 <set> 5 <bean class="org.fkit.converter.StringToDateConverter"></bean> 6 </set> 7 </property> 8 </bean>