在作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>