|
|
|
@ -1693,18 +1693,18 @@ See <<web.adoc#mvc-config-conversion, Conversion and Formatting>> in the Spring |
|
|
|
[[format-configuring-formatting-globaldatetimeformat]] |
|
|
|
[[format-configuring-formatting-globaldatetimeformat]] |
|
|
|
== Configuring a Global Date and Time Format |
|
|
|
== Configuring a Global Date and Time Format |
|
|
|
|
|
|
|
|
|
|
|
By default, date and time fields that are not annotated with `@DateTimeFormat` are |
|
|
|
By default, date and time fields not annotated with `@DateTimeFormat` are converted from |
|
|
|
converted from strings by using the `DateFormat.SHORT` style. If you prefer, you can |
|
|
|
strings by using the `DateFormat.SHORT` style. If you prefer, you can change this by |
|
|
|
change this by defining your own global format. |
|
|
|
defining your own global format. |
|
|
|
|
|
|
|
|
|
|
|
To do so, you need to ensure that Spring does not register default formatters. Instead, |
|
|
|
To do that, ensure that Spring does not register default formatters. Instead, register |
|
|
|
you should register all formatters manually. Use the |
|
|
|
formatters manually with the help of: |
|
|
|
`org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar` or |
|
|
|
|
|
|
|
`org.springframework.format.datetime.DateFormatterRegistrar` class, depending on whether |
|
|
|
|
|
|
|
you use the Joda-Time library. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example, the following Java configuration registers a global `yyyyMMdd` |
|
|
|
* `org.springframework.format.datetime.standard.DateTimeFormatterRegistrar` |
|
|
|
format (this example does not depend on the Joda-Time library): |
|
|
|
* `org.springframework.format.datetime.DateFormatterRegistrar`, or |
|
|
|
|
|
|
|
`org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar` for Joda-Time. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For example, the following Java configuration registers a global `yyyyMMdd` format: |
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
[source,java,indent=0,subs="verbatim,quotes",role="primary"] |
|
|
|
.Java |
|
|
|
.Java |
|
|
|
@ -1721,6 +1721,11 @@ format (this example does not depend on the Joda-Time library): |
|
|
|
// Ensure @NumberFormat is still supported |
|
|
|
// Ensure @NumberFormat is still supported |
|
|
|
conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); |
|
|
|
conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register JSR-310 date conversion with a specific global format |
|
|
|
|
|
|
|
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); |
|
|
|
|
|
|
|
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")); |
|
|
|
|
|
|
|
registrar.registerFormatters(conversionService); |
|
|
|
|
|
|
|
|
|
|
|
// Register date conversion with a specific global format |
|
|
|
// Register date conversion with a specific global format |
|
|
|
DateFormatterRegistrar registrar = new DateFormatterRegistrar(); |
|
|
|
DateFormatterRegistrar registrar = new DateFormatterRegistrar(); |
|
|
|
registrar.setFormatter(new DateFormatter("yyyyMMdd")); |
|
|
|
registrar.setFormatter(new DateFormatter("yyyyMMdd")); |
|
|
|
@ -1740,8 +1745,15 @@ format (this example does not depend on the Joda-Time library): |
|
|
|
fun conversionService(): FormattingConversionService { |
|
|
|
fun conversionService(): FormattingConversionService { |
|
|
|
// Use the DefaultFormattingConversionService but do not register defaults |
|
|
|
// Use the DefaultFormattingConversionService but do not register defaults |
|
|
|
return DefaultFormattingConversionService(false).apply { |
|
|
|
return DefaultFormattingConversionService(false).apply { |
|
|
|
|
|
|
|
|
|
|
|
// Ensure @NumberFormat is still supported |
|
|
|
// Ensure @NumberFormat is still supported |
|
|
|
addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory()) |
|
|
|
addFormatterForFieldAnnotation(NumberFormatAnnotationFormatterFactory()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Register JSR-310 date conversion with a specific global format |
|
|
|
|
|
|
|
val registrar = DateTimeFormatterRegistrar() |
|
|
|
|
|
|
|
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd")) |
|
|
|
|
|
|
|
registrar.registerFormatters(this) |
|
|
|
|
|
|
|
|
|
|
|
// Register date conversion with a specific global format |
|
|
|
// Register date conversion with a specific global format |
|
|
|
val registrar = DateFormatterRegistrar() |
|
|
|
val registrar = DateFormatterRegistrar() |
|
|
|
registrar.setFormatter(DateFormatter("yyyyMMdd")) |
|
|
|
registrar.setFormatter(DateFormatter("yyyyMMdd")) |
|
|
|
@ -1786,18 +1798,10 @@ Time): |
|
|
|
</beans> |
|
|
|
</beans> |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
NOTE: Joda-Time provides separate distinct types to represent `date`, `time`, and `date-time` |
|
|
|
Note there are extra considerations when configuring date and time formats in web |
|
|
|
values. The `dateFormatter`, `timeFormatter`, and `dateTimeFormatter` properties of the |
|
|
|
applications. Please see |
|
|
|
`JodaTimeFormatterRegistrar` should be used to configure the different formats for each |
|
|
|
<<web.adoc#mvc-config-conversion, WebMVC Conversion and Formatting>> or |
|
|
|
type. The `DateTimeFormatterFactoryBean` provides a convenient way to create formatters. |
|
|
|
<<web-reactive.adoc#webflux-config-conversion, WebFlux Conversion and Formatting>>. |
|
|
|
|
|
|
|
|
|
|
|
NOTE: If you use Spring MVC, remember to explicitly configure the conversion service that |
|
|
|
|
|
|
|
is used. For Java-based `@Configuration`, this means extending the |
|
|
|
|
|
|
|
`WebMvcConfigurationSupport` class and overriding the `mvcConversionService()` method. |
|
|
|
|
|
|
|
For XML, you should use the `conversion-service` attribute of the |
|
|
|
|
|
|
|
`mvc:annotation-driven` element. |
|
|
|
|
|
|
|
See <<web.adoc#mvc-config-conversion, Conversion and Formatting>> for details. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|