Add support for Instant in @DateTimeFormat

Closes gh-19846
This commit is contained in:
Stephane Nicoll
2021-12-03 16:17:52 +01:00
parent 0a41da9ec9
commit 110e0f7f2b
3 changed files with 27 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.format.datetime.standard;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@@ -57,6 +58,7 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
static {
// Create the set of field types that may be annotated with @DateTimeFormat.
Set<Class<?>> fieldTypes = new HashSet<>(8);
fieldTypes.add(Instant.class);
fieldTypes.add(LocalDate.class);
fieldTypes.add(LocalTime.class);
fieldTypes.add(LocalDateTime.class);

View File

@@ -17,6 +17,7 @@
package org.springframework.format.datetime.standard;
import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@@ -115,7 +116,10 @@ public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
private TemporalAccessor doParse(String text, Locale locale, DateTimeFormatter formatter) throws DateTimeParseException {
DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(formatter, locale);
if (LocalDate.class == this.temporalAccessorType) {
if (Instant.class == this.temporalAccessorType) {
return formatterToUse.parse(text, Instant::from);
}
else if (LocalDate.class == this.temporalAccessorType) {
return LocalDate.parse(text, formatterToUse);
}
else if (LocalTime.class == this.temporalAccessorType) {