diff --git a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java index 35f361fe654..2c165c5c0fe 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatterRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,7 @@ import org.springframework.util.Assert; /** * Configures basic date formatting for use with Spring, primarily for * {@link org.springframework.format.annotation.DateTimeFormat} declarations. - * Applies to fields of type {@link Date}, {@link Calendar} and {@code long}. + * Applies to fields of type {@link Date}, {@link Calendar}, and {@code long}. * *

Designed for direct instantiation but also exposes the static * {@link #addDateConverters(ConverterRegistry)} utility method for diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java index 6ef3f278074..eb1a7dc877a 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterRegistrar.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -77,8 +77,8 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { /** * Set whether standard ISO formatting should be applied to all date/time types. - * Default is "false" (no). - *

If set to "true", the "dateStyle", "timeStyle" and "dateTimeStyle" + *

Default is "false" (no). + *

If set to "true", the "dateStyle", "timeStyle", and "dateTimeStyle" * properties are effectively ignored. */ public void setUseIsoFormat(boolean useIsoFormat) { @@ -89,7 +89,7 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { /** * Set the default format style of {@link java.time.LocalDate} objects. - * Default is {@link java.time.format.FormatStyle#SHORT}. + *

Default is {@link java.time.format.FormatStyle#SHORT}. */ public void setDateStyle(FormatStyle dateStyle) { this.factories.get(Type.DATE).setDateStyle(dateStyle); @@ -97,7 +97,7 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { /** * Set the default format style of {@link java.time.LocalTime} objects. - * Default is {@link java.time.format.FormatStyle#SHORT}. + *

Default is {@link java.time.format.FormatStyle#SHORT}. */ public void setTimeStyle(FormatStyle timeStyle) { this.factories.get(Type.TIME).setTimeStyle(timeStyle); @@ -105,7 +105,7 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { /** * Set the default format style of {@link java.time.LocalDateTime} objects. - * Default is {@link java.time.format.FormatStyle#SHORT}. + *

Default is {@link java.time.format.FormatStyle#SHORT}. */ public void setDateTimeStyle(FormatStyle dateTimeStyle) { this.factories.get(Type.DATE_TIME).setDateTimeStyle(dateTimeStyle); @@ -139,7 +139,7 @@ public class DateTimeFormatterRegistrar implements FormatterRegistrar { /** * Set the formatter that will be used for objects representing date and time values. - *

This formatter will be used for {@link LocalDateTime}, {@link ZonedDateTime} + *

This formatter will be used for {@link LocalDateTime}, {@link ZonedDateTime}, * and {@link OffsetDateTime} types. When specified, the * {@link #setDateTimeStyle dateTimeStyle} and * {@link #setUseIsoFormat useIsoFormat} properties will be ignored. diff --git a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterUtils.java b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterUtils.java index be767f0e23c..3aff5d779dd 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterUtils.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,10 +29,18 @@ import org.springframework.util.StringUtils; */ abstract class DateTimeFormatterUtils { + /** + * Create a {@link DateTimeFormatter} for the supplied pattern, configured with + * {@linkplain ResolverStyle#STRICT strict} resolution. + *

Note that the strict resolution does not affect the parsing. + * @param pattern the pattern to use + * @return a new {@code DateTimeFormatter} + * @see ResolverStyle#STRICT + */ static DateTimeFormatter createStrictDateTimeFormatter(String pattern) { - // Using strict parsing to align with Joda-Time and standard DateFormat behavior: + // Using strict resolution to align with Joda-Time and standard DateFormat behavior: // otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected. - // However, with strict parsing, a year digit needs to be specified as 'u'... + // However, with strict resolution, a year digit needs to be specified as 'u'... String patternToUse = StringUtils.replace(pattern, "yy", "uu"); return DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT); } diff --git a/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java b/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java index 6f7751545e3..b45a4a28c2c 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/DateFormattingTests.java @@ -61,12 +61,11 @@ class DateFormattingTests { @BeforeEach void setup() { - DateFormatterRegistrar registrar = new DateFormatterRegistrar(); - setup(registrar); + DefaultConversionService.addDefaultConverters(conversionService); + setup(new DateFormatterRegistrar()); } private void setup(DateFormatterRegistrar registrar) { - DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); SimpleDateBean bean = new SimpleDateBean(); @@ -172,7 +171,7 @@ class DateFormattingTests { @Test @Disabled void testBindDateAnnotatedWithFallbackError() { - // TODO This currently passes because of the Date(String) constructor fallback is used + // TODO This currently passes because the Date(String) constructor fallback is used MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("styleDate", "Oct 031, 2009"); binder.bind(propertyValues); @@ -181,7 +180,7 @@ class DateFormattingTests { } @Test - void testBindDateAnnotatedPattern() { + void testBindDateTimePatternAnnotated() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("patternDate", "10/31/09 1:05"); binder.bind(propertyValues); @@ -190,7 +189,7 @@ class DateFormattingTests { } @Test - void testBindDateAnnotatedPatternWithGlobalFormat() { + void testBindDateTimePatternAnnotatedWithGlobalFormat() { DateFormatterRegistrar registrar = new DateFormatterRegistrar(); DateFormatter dateFormatter = new DateFormatter(); dateFormatter.setIso(ISO.DATE_TIME); @@ -205,7 +204,7 @@ class DateFormattingTests { } @Test - void testBindDateTimeOverflow() { + void testBindDateTimePatternAnnotatedWithOverflow() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("patternDate", "02/29/09 12:00 PM"); binder.bind(propertyValues); diff --git a/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormatterFactoryBeanTests.java b/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormatterFactoryBeanTests.java index 0f1afed6aed..fc2277bee8b 100644 --- a/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormatterFactoryBeanTests.java +++ b/spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormatterFactoryBeanTests.java @@ -23,10 +23,6 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; - - - - /** * @author Phillip Webb * @author Sam Brannen