配置全局日期和时间格式

默认情况下,未使用@DateTimeFormat注释的日期和时间字段将使用DateFormat.SHORT样式从字符串转换。如果您希望,可以通过定义自己的全局格式来更改这一点。

为此,请确保Spring不注册默认格式化程序。而是通过手动注册格式化程序来帮助:

  • org.springframework.format.datetime.standard.DateTimeFormatterRegistrar

  • org.springframework.format.datetime.DateFormatterRegistrar

例如,以下Java配置注册了一个全局yyyyMMdd格式:

  • Java

  • Kotlin

@Configuration
public class AppConfig {

	@Bean
	public FormattingConversionService conversionService() {

		// 使用DefaultFormattingConversionService,但不注册默认值
		DefaultFormattingConversionService conversionService =
			new DefaultFormattingConversionService(false);

		// 确保仍支持@NumberFormat
		conversionService.addFormatterForFieldAnnotation(
			new NumberFormatAnnotationFormatterFactory());

		// 使用特定全局格式注册JSR-310日期转换
		DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();
		dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
		dateTimeRegistrar.registerFormatters(conversionService);

		// 使用特定全局格式注册日期转换
		DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();
		dateRegistrar.setFormatter(new DateFormatter("yyyyMMdd"));
		dateRegistrar.registerFormatters(conversionService);

		return conversionService;
	}
}
@Configuration
class AppConfig {

	@Bean
	fun conversionService(): FormattingConversionService {
		// 使用DefaultFormattingConversionService,但不注册默认值
		return DefaultFormattingConversionService(false).apply {

			// 确保仍支持@NumberFormat
			addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory())

			// 使用特定全局格式注册JSR-310日期转换
			val dateTimeRegistrar = DateTimeFormatterRegistrar()
			dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"))
			dateTimeRegistrar.registerFormatters(this)

			// 使用特定全局格式注册日期转换
			val dateRegistrar = DateFormatterRegistrar()
			dateRegistrar.setFormatter(DateFormatter("yyyyMMdd"))
			dateRegistrar.registerFormatters(this)
		}
	}
}

如果您更喜欢基于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/beans
		https://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转换和格式化