在作web开发的时候,页面传入的都是String类型,SpringMVC能够对一些基本的类型进行转换,可是对于日期类的转换可能就须要咱们配置。web
一、若是查询类使咱们本身写,那么在属性前面加上@DateTimeFormat(pattern = "yyyy-MM-dd") ,便可将String转换为Date类型,以下spring
@DateTimeFormat(pattern = "yyyy-MM-dd") private Date createTime;
2 、能够在系统中加入一个全局类型转换器ide
实现转换器orm
1 public class DateConverter implements Converter<String, Date> { 2 @Override 3 public Date convert(String source) { 4 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 5 dateFormat.setLenient(false); 6 try { 7 return dateFormat.parse(source); 8 } catch (ParseException e) { 9 e.printStackTrace(); 10 } 11 return null; 12 }
进行配置:开发
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="com.doje.XXX.web.DateConverter" /> </list> </property> </bean>