不同的字段需要的日期pattern不同,看了下,spring3.2原生支持
字段加入注解
@DateTimeFormat(pattern="yyyy/MM/dd hh:mm:ss") private Date date; @DateTimeFormat(pattern="yyyy/MM/dd hh:mm") private Date endDate;
不使用joda-time,配置方式:
<mvc:annotation-driven conversion-service="conversionService" /> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="registerDefaultFormatters" value="false" /> <property name="formatterRegistrars"> <set> <bean class="org.springframework.format.datetime.DateFormatterRegistrar"> <property name="formatter" ref="dateFormatter"> </property> </bean> </set> </property> </bean> <bean id="dateFormatter" class="org.springframework.format.datetime.DateFormatter"> <property name="pattern" value="yyyy/MM/dd"></property> </bean>
使用joda-time如下配置
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="registerDefaultFormatters" value="false" /> <property name="formatters"> <set> <bean class="org.springframework.format.number.NumberFormatAnnotationFormatterFactory" /> </set> </property> <property name="formatterRegistrars"> <set> <bean class="org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar"> <property name="dateFormatter"> <bean class="org.springframework.format.datetime.joda.DateTimeFormatterFactoryBean"> <property name="pattern" value="yyyyMMdd"/> </bean> </property> </bean> </set> </property> </bean>