默认情况下,没有用 @DateTimeFormat注解的日期和时间字段通过使用 DateFormat.SHORT样式从字符串转换。如果你愿意,你可以通过定义你自己的全局格式来改变这一点。
要做到这一点,请确保 Spring 不注册默认的格式化器。相反,在以下工具的帮助下手动注册格式化器:
org.springframework.format.datetime.standard.DateTimeFormatterRegistrarorg.springframework.format.datetime.DateFormatterRegistrar
例如,下面的 Java 配置注册了一个全局 YYYMMDD 格式:
package cn.mrcode.study.springdocsread.web;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.support.ConversionServiceFactoryBean;import org.springframework.core.convert.support.GenericConversionService;import org.springframework.format.datetime.DateFormatter;import org.springframework.format.datetime.DateFormatterRegistrar;import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;import org.springframework.format.support.DefaultFormattingConversionService;import org.springframework.format.support.FormattingConversionService;import java.time.format.DateTimeFormatter;import javax.annotation.PostConstruct;/*** @author mrcode*/@Configurationpublic class AppConfig {@Beanpublic FormattingConversionService conversionService() {// 使用 DefaultFormattingConversionService 但不注册默认值DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);// 确保 @NumberFormat 仍被支持conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());//用特定的全局格式注册 JSR-310 日期转换DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));registrar.registerFormatters(conversionService);// 用特定的全局格式进行注册日期转换DateFormatterRegistrar registrar = new DateFormatterRegistrar();registrar.setFormatter(new DateFormatter("yyyyMMdd"));registrar.registerFormatters(conversionService);// 上面可以二选一return conversionService;}}
如果你喜欢基于 XML 的配置,你可以使用一个 FormattingConversionServiceFactoryBean。下面的例子显示了如何做到这一点:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd><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.standard.DateTimeFormatterRegistrar"><property name="dateFormatter"><bean class="org.springframework.format.datetime.standard.DateTimeFormatterFactoryBean"><property name="pattern" value="yyyyMMdd"/></bean></property></bean></set></property></bean></beans>
注意在 Web 应用程序中配置日期和时间格式时有额外的注意事项。请参阅 WebMVC 转换和格式化 或 WebFlux 转换和格式化。
