打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring mvc 支持注解的日期转换配置

Spring mvc 支持注解的日期转换 配置

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

Spring mvc 支持注解的日期转换 配置

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java常用的时间工具类DateTimeUtils.java对常用的时间操作方法总结
Joda
@JsonFormat与@DateTimeFormat注解的使用
DateTime.ToString() Patterns
ANT获取时间
jstl 格式化时间,数字方法集合
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服