@@ -1693,18 +1693,18 @@ See <<web.adoc#mvc-config-conversion, Conversion and Formatting>> in the Spring
16931693[[format-configuring-formatting-globaldatetimeformat]]
16941694== Configuring a Global Date and Time Format
16951695
1696- By default, date and time fields that are not annotated with `@DateTimeFormat` are
1697- converted from strings by using the `DateFormat.SHORT` style. If you prefer, you can
1698- change this by defining your own global format.
1696+ By default, date and time fields not annotated with `@DateTimeFormat` are converted from
1697+ strings by using the `DateFormat.SHORT` style. If you prefer, you can change this by
1698+ defining your own global format.
16991699
1700- To do so, you need to ensure that Spring does not register default formatters. Instead,
1701- you should register all formatters manually. Use the
1702- `org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar` or
1703- `org.springframework.format.datetime.DateFormatterRegistrar` class, depending on whether
1704- you use the Joda-Time library.
1700+ To do that, ensure that Spring does not register default formatters. Instead, register
1701+ formatters manually with the help of:
17051702
1706- For example, the following Java configuration registers a global `yyyyMMdd`
1707- format (this example does not depend on the Joda-Time library):
1703+ * `org.springframework.format.datetime.standard.DateTimeFormatterRegistrar`
1704+ * `org.springframework.format.datetime.DateFormatterRegistrar`, or
1705+ `org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar` for Joda-Time.
1706+
1707+ For example, the following Java configuration registers a global `yyyyMMdd` format:
17081708
17091709[source,java,indent=0,subs="verbatim,quotes",role="primary"]
17101710.Java
@@ -1721,6 +1721,11 @@ format (this example does not depend on the Joda-Time library):
17211721 // Ensure @NumberFormat is still supported
17221722 conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
17231723
1724+ // Register JSR-310 date conversion with a specific global format
1725+ DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
1726+ registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
1727+ registrar.registerFormatters(conversionService);
1728+
17241729 // Register date conversion with a specific global format
17251730 DateFormatterRegistrar registrar = new DateFormatterRegistrar();
17261731 registrar.setFormatter(new DateFormatter("yyyyMMdd"));
@@ -1740,8 +1745,15 @@ format (this example does not depend on the Joda-Time library):
17401745 fun conversionService(): FormattingConversionService {
17411746 // Use the DefaultFormattingConversionService but do not register defaults
17421747 return DefaultFormattingConversionService(false).apply {
1748+
17431749 // Ensure @NumberFormat is still supported
17441750 addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory())
1751+
1752+ // Register JSR-310 date conversion with a specific global format
1753+ val registrar = DateTimeFormatterRegistrar()
1754+ registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"))
1755+ registrar.registerFormatters(this)
1756+
17451757 // Register date conversion with a specific global format
17461758 val registrar = DateFormatterRegistrar()
17471759 registrar.setFormatter(DateFormatter("yyyyMMdd"))
@@ -1786,18 +1798,10 @@ Time):
17861798 </beans>
17871799----
17881800
1789- NOTE: Joda-Time provides separate distinct types to represent `date`, `time`, and `date-time`
1790- values. The `dateFormatter`, `timeFormatter`, and `dateTimeFormatter` properties of the
1791- `JodaTimeFormatterRegistrar` should be used to configure the different formats for each
1792- type. The `DateTimeFormatterFactoryBean` provides a convenient way to create formatters.
1793-
1794- NOTE: If you use Spring MVC, remember to explicitly configure the conversion service that
1795- is used. For Java-based `@Configuration`, this means extending the
1796- `WebMvcConfigurationSupport` class and overriding the `mvcConversionService()` method.
1797- For XML, you should use the `conversion-service` attribute of the
1798- `mvc:annotation-driven` element.
1799- See <<web.adoc#mvc-config-conversion, Conversion and Formatting>> for details.
1800-
1801+ Note there are extra considerations when configuring date and time formats in web
1802+ applications. Please see
1803+ <<web.adoc#mvc-config-conversion, WebMVC Conversion and Formatting>> or
1804+ <<web-reactive.adoc#webflux-config-conversion, WebFlux Conversion and Formatting>>.
18011805
18021806
18031807
0 commit comments