Fallback to ISO-based default java.time type parsing
Closes gh-26985
This commit is contained in:
parent
3d83db6abb
commit
f4dfe94702
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2021 the original author or authors.
|
* Copyright 2002-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -77,7 +77,8 @@ public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
|
||||||
}
|
}
|
||||||
|
|
||||||
TemporalAccessorParser(Class<? extends TemporalAccessor> temporalAccessorType, DateTimeFormatter formatter,
|
TemporalAccessorParser(Class<? extends TemporalAccessor> temporalAccessorType, DateTimeFormatter formatter,
|
||||||
@Nullable String[] fallbackPatterns, @Nullable Object source) {
|
@Nullable String[] fallbackPatterns, @Nullable Object source) {
|
||||||
|
|
||||||
this.temporalAccessorType = temporalAccessorType;
|
this.temporalAccessorType = temporalAccessorType;
|
||||||
this.formatter = formatter;
|
this.formatter = formatter;
|
||||||
this.fallbackPatterns = fallbackPatterns;
|
this.fallbackPatterns = fallbackPatterns;
|
||||||
|
|
@ -104,10 +105,19 @@ public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
// Fallback to ISO-based default java.time type parsing
|
||||||
|
try {
|
||||||
|
return defaultParse(text);
|
||||||
|
}
|
||||||
|
catch (DateTimeParseException ignoredException) {
|
||||||
|
// Ignore fallback parsing exception like above
|
||||||
|
}
|
||||||
|
}
|
||||||
if (this.source != null) {
|
if (this.source != null) {
|
||||||
throw new DateTimeParseException(
|
throw new DateTimeParseException(
|
||||||
String.format("Unable to parse date time value \"%s\" using configuration from %s", text, this.source),
|
String.format("Unable to parse date time value \"%s\" using configuration from %s", text, this.source),
|
||||||
text, ex.getErrorIndex(), ex);
|
text, ex.getErrorIndex(), ex);
|
||||||
}
|
}
|
||||||
// else rethrow original exception
|
// else rethrow original exception
|
||||||
throw ex;
|
throw ex;
|
||||||
|
|
@ -148,4 +158,37 @@ public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private TemporalAccessor defaultParse(String text) throws DateTimeParseException {
|
||||||
|
if (Instant.class == this.temporalAccessorType) {
|
||||||
|
return Instant.parse(text);
|
||||||
|
}
|
||||||
|
else if (LocalDate.class == this.temporalAccessorType) {
|
||||||
|
return LocalDate.parse(text);
|
||||||
|
}
|
||||||
|
else if (LocalTime.class == this.temporalAccessorType) {
|
||||||
|
return LocalTime.parse(text);
|
||||||
|
}
|
||||||
|
else if (LocalDateTime.class == this.temporalAccessorType) {
|
||||||
|
return LocalDateTime.parse(text);
|
||||||
|
}
|
||||||
|
else if (ZonedDateTime.class == this.temporalAccessorType) {
|
||||||
|
return ZonedDateTime.parse(text);
|
||||||
|
}
|
||||||
|
else if (OffsetDateTime.class == this.temporalAccessorType) {
|
||||||
|
return OffsetDateTime.parse(text);
|
||||||
|
}
|
||||||
|
else if (OffsetTime.class == this.temporalAccessorType) {
|
||||||
|
return OffsetTime.parse(text);
|
||||||
|
}
|
||||||
|
else if (YearMonth.class == this.temporalAccessorType) {
|
||||||
|
return YearMonth.parse(text);
|
||||||
|
}
|
||||||
|
else if (MonthDay.class == this.temporalAccessorType) {
|
||||||
|
return MonthDay.parse(text);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new IllegalStateException("Unsupported TemporalAccessor type: " + this.temporalAccessorType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,6 +110,15 @@ class DateTimeFormattingTests {
|
||||||
assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09");
|
assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBindLocalDateWithISO() {
|
||||||
|
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||||
|
propertyValues.add("localDate", "2009-10-31");
|
||||||
|
binder.bind(propertyValues);
|
||||||
|
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
|
||||||
|
assertThat(binder.getBindingResult().getFieldValue("localDate")).isEqualTo("10/31/09");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBindLocalDateWithSpecificStyle() {
|
void testBindLocalDateWithSpecificStyle() {
|
||||||
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
|
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
|
||||||
|
|
@ -207,6 +216,15 @@ class DateTimeFormattingTests {
|
||||||
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
|
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBindLocalTimeWithISO() {
|
||||||
|
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||||
|
propertyValues.add("localTime", "12:00:00");
|
||||||
|
binder.bind(propertyValues);
|
||||||
|
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
|
||||||
|
assertThat(binder.getBindingResult().getFieldValue("localTime")).isEqualTo("12:00 PM");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBindLocalTimeWithSpecificStyle() {
|
void testBindLocalTimeWithSpecificStyle() {
|
||||||
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
|
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
|
||||||
|
|
@ -260,6 +278,17 @@ class DateTimeFormattingTests {
|
||||||
assertThat(value.endsWith("12:00 PM")).isTrue();
|
assertThat(value.endsWith("12:00 PM")).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBindLocalDateTimeWithISO() {
|
||||||
|
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||||
|
propertyValues.add("localDateTime", "2009-10-31T12:00:00");
|
||||||
|
binder.bind(propertyValues);
|
||||||
|
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
|
||||||
|
String value = binder.getBindingResult().getFieldValue("localDateTime").toString();
|
||||||
|
assertThat(value.startsWith("10/31/09")).isTrue();
|
||||||
|
assertThat(value.endsWith("12:00 PM")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBindLocalDateTimeAnnotated() {
|
void testBindLocalDateTimeAnnotated() {
|
||||||
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
MutablePropertyValues propertyValues = new MutablePropertyValues();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue