e2

滴滴侠,fai抖

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

不同的字段需要的日期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>

 

posted on 2017-03-22 22:57  纯黑Se丶  阅读(257)  评论(0编辑  收藏  举报