Improve docs on date and time formatting
Closes gh-24370
This commit is contained in:
@@ -3691,11 +3691,10 @@ class WebConfig : WebFluxConfigurer {
|
||||
=== Conversion, formatting
|
||||
[.small]#<<web.adoc#mvc-config-conversion, Web MVC>>#
|
||||
|
||||
By default, formatters for `Number` and `Date` types are installed, including support for
|
||||
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda-Time
|
||||
formatting library is also installed if Joda-Time is present on the classpath.
|
||||
By default, formatters for various number and date types are installed, along with support
|
||||
for customization via `@NumberFormat` and `@DateTimeFormat` on fields.
|
||||
|
||||
The following example shows how to register custom formatters and converters:
|
||||
To register custom formatters and converters in Java config, use the following:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
@@ -3724,6 +3723,41 @@ The following example shows how to register custom formatters and converters:
|
||||
}
|
||||
----
|
||||
|
||||
By default Spring WebFlux considers the request Locale when parsing and formatting date
|
||||
values. This works for forms where dates are represented as Strings with "input" form
|
||||
fields. For "date" and "time" form fields, however, browsers use a fixed format defined
|
||||
in the HTML spec. For such cases date and time formatting can be customized as follows:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
----
|
||||
@Configuration
|
||||
@EnableWebFlux
|
||||
public class WebConfig implements WebFluxConfigurer {
|
||||
|
||||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
|
||||
registrar.setUseIsoFormat(true);
|
||||
registrar.registerFormatters(registry);
|
||||
}
|
||||
}
|
||||
----
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
@Configuration
|
||||
@EnableWebFlux
|
||||
class WebConfig : WebFluxConfigurer {
|
||||
|
||||
override fun addFormatters(registry: FormatterRegistry) {
|
||||
val registrar = DateTimeFormatterRegistrar()
|
||||
registrar.setUseIsoFormat(true)
|
||||
registrar.registerFormatters(registry)
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
NOTE: See <<core.adoc#format-FormatterRegistrar-SPI, `FormatterRegistrar` SPI>>
|
||||
and the `FormattingConversionServiceFactoryBean` for more information on when to
|
||||
use `FormatterRegistrar` implementations.
|
||||
|
||||
@@ -4977,12 +4977,10 @@ sub-elements are available.
|
||||
=== Type Conversion
|
||||
[.small]#<<web-reactive.adoc#webflux-config-conversion, WebFlux>>#
|
||||
|
||||
By default formatters, for `Number` and `Date` types are installed, including support for
|
||||
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda-Time
|
||||
formatting library is also installed if Joda-Time is present on the classpath.
|
||||
By default, formatters for various number and date types are installed, along with support
|
||||
for customization via `@NumberFormat` and `@DateTimeFormat` on fields.
|
||||
|
||||
In Java configuration, you can register custom formatters and converters, as the
|
||||
following example shows:
|
||||
To register custom formatters and converters in Java config, use the following:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
@@ -5010,7 +5008,7 @@ following example shows:
|
||||
}
|
||||
----
|
||||
|
||||
The following example shows how to achieve the same configuration in XML:
|
||||
To do the same in XML config, use the following:
|
||||
|
||||
[source,xml,indent=0,subs="verbatim,quotes"]
|
||||
----
|
||||
@@ -5049,6 +5047,41 @@ The following example shows how to achieve the same configuration in XML:
|
||||
</beans>
|
||||
----
|
||||
|
||||
By default Spring MVC considers the request Locale when parsing and formatting date
|
||||
values. This works for forms where dates are represented as Strings with "input" form
|
||||
fields. For "date" and "time" form fields, however, browsers use a fixed format defined
|
||||
in the HTML spec. For such cases date and time formatting can be customized as follows:
|
||||
|
||||
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
|
||||
.Java
|
||||
----
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
|
||||
registrar.setUseIsoFormat(true);
|
||||
registrar.registerFormatters(registry);
|
||||
}
|
||||
}
|
||||
----
|
||||
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
|
||||
.Kotlin
|
||||
----
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
class WebConfig : WebMvcConfigurer {
|
||||
|
||||
override fun addFormatters(registry: FormatterRegistry) {
|
||||
val registrar = DateTimeFormatterRegistrar()
|
||||
registrar.setUseIsoFormat(true)
|
||||
registrar.registerFormatters(registry)
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
NOTE: See <<core.adoc#format-FormatterRegistrar-SPI, the `FormatterRegistrar` SPI>>
|
||||
and the `FormattingConversionServiceFactoryBean` for more information on when to use
|
||||
FormatterRegistrar implementations.
|
||||
|
||||
Reference in New Issue
Block a user