@@ -1693,18 +1693,18 @@ See <<web.adoc#mvc-config-conversion, Conversion and Formatting>> in the Spring
1693
1693
[[format-configuring-formatting-globaldatetimeformat]]
1694
1694
== Configuring a Global Date and Time Format
1695
1695
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.
1699
1699
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:
1705
1702
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:
1708
1708
1709
1709
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
1710
1710
.Java
@@ -1721,6 +1721,11 @@ format (this example does not depend on the Joda-Time library):
1721
1721
// Ensure @NumberFormat is still supported
1722
1722
conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
1723
1723
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
+
1724
1729
// Register date conversion with a specific global format
1725
1730
DateFormatterRegistrar registrar = new DateFormatterRegistrar();
1726
1731
registrar.setFormatter(new DateFormatter("yyyyMMdd"));
@@ -1740,8 +1745,15 @@ format (this example does not depend on the Joda-Time library):
1740
1745
fun conversionService(): FormattingConversionService {
1741
1746
// Use the DefaultFormattingConversionService but do not register defaults
1742
1747
return DefaultFormattingConversionService(false).apply {
1748
+
1743
1749
// Ensure @NumberFormat is still supported
1744
1750
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
+
1745
1757
// Register date conversion with a specific global format
1746
1758
val registrar = DateFormatterRegistrar()
1747
1759
registrar.setFormatter(DateFormatter("yyyyMMdd"))
@@ -1786,18 +1798,10 @@ Time):
1786
1798
</beans>
1787
1799
----
1788
1800
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>>.
1801
1805
1802
1806
1803
1807
0 commit comments