Polishing

This commit is contained in:
Sam Brannen 2024-07-05 14:21:41 +02:00
parent 0544dfe090
commit 83f7996c79
5 changed files with 26 additions and 23 deletions

View File

@ -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}.
*
* <p>Designed for direct instantiation but also exposes the static
* {@link #addDateConverters(ConverterRegistry)} utility method for

View File

@ -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).
* <p>If set to "true", the "dateStyle", "timeStyle" and "dateTimeStyle"
* <p>Default is "false" (no).
* <p>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}.
* <p>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}.
* <p>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}.
* <p>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.
* <p>This formatter will be used for {@link LocalDateTime}, {@link ZonedDateTime}
* <p>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.

View File

@ -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.
* <p>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);
}

View File

@ -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);

View File

@ -23,10 +23,6 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Phillip Webb
* @author Sam Brannen