Skip to content

Commit

Permalink
Add support for Instant in @DateTimeFormat
Browse files Browse the repository at this point in the history
Closes gh-19846
  • Loading branch information
snicoll committed Dec 3, 2021
1 parent 0a41da9 commit 110e0f7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,7 +116,10 @@ public TemporalAccessor parse(String text, Locale locale) throws ParseException

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ void testBindInstant() {
assertThat(binder.getBindingResult().getFieldValue("instant").toString().startsWith("2009-10-31T12:00")).isTrue();
}

@Test
void testBindInstantAnnotated() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("styleInstant", "2017-02-21T13:00");
binder.bind(propertyValues);
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
assertThat(binder.getBindingResult().getFieldValue("styleInstant")).isEqualTo("2017-02-21T13:00");
}

@Test
@SuppressWarnings("deprecation")
void testBindInstantFromJavaUtilDate() {
Expand Down Expand Up @@ -622,6 +631,9 @@ public static class DateTimeBean {

private Instant instant;

@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private Instant styleInstant;

private Period period;

private Duration duration;
Expand Down Expand Up @@ -762,6 +774,14 @@ public void setInstant(Instant instant) {
this.instant = instant;
}

public Instant getStyleInstant() {
return this.styleInstant;
}

public void setStyleInstant(Instant styleInstant) {
this.styleInstant = styleInstant;
}

public Period getPeriod() {
return this.period;
}
Expand Down

0 comments on commit 110e0f7

Please sign in to comment.