From 83f7996c79970aab5d9fa5b63c22e7f45f30b6b6 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Fri, 5 Jul 2024 14:21:41 +0200 Subject: [PATCH] Polishing --- .../format/datetime/DateFormatterRegistrar.java | 4 ++-- .../standard/DateTimeFormatterRegistrar.java | 14 +++++++------- .../datetime/standard/DateTimeFormatterUtils.java | 14 +++++++++++--- .../format/datetime/DateFormattingTests.java | 13 ++++++------- .../DateTimeFormatterFactoryBeanTests.java | 4 ---- 5 files changed, 26 insertions(+), 23 deletions(-) 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 35f361fe65..2c165c5c0f 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 1a72d44949..3a340be6ba 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. @@ -76,8 +76,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) { @@ -88,7 +88,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); @@ -96,7 +96,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); @@ -104,7 +104,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); @@ -138,7 +138,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 be767f0e23..3aff5d779d 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 6f7751545e..b45a4a28c2 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 0f1afed6ae..fc2277bee8 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