Skip to content

Commit

Permalink
Add support for YearMonth and MonthDay in @DateTimeFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo authored and snicoll committed Dec 3, 2021
1 parent a9d2016 commit 65eceaf
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.MonthDay;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
Expand All @@ -43,6 +45,7 @@
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Kazuki Shimizu
* @since 4.0
* @see org.springframework.format.annotation.DateTimeFormat
*/
Expand All @@ -60,6 +63,8 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
fieldTypes.add(ZonedDateTime.class);
fieldTypes.add(OffsetDateTime.class);
fieldTypes.add(OffsetTime.class);
fieldTypes.add(YearMonth.class);
fieldTypes.add(MonthDay.class);
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand All @@ -38,6 +40,7 @@
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Kazuki Shimizu
* @since 4.0
* @see DateTimeContextHolder#getFormatter
* @see java.time.LocalDate#parse(CharSequence, java.time.format.DateTimeFormatter)
Expand All @@ -46,6 +49,8 @@
* @see java.time.ZonedDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
* @see java.time.OffsetDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
* @see java.time.OffsetTime#parse(CharSequence, java.time.format.DateTimeFormatter)
* @see java.time.YearMonth#parse(CharSequence, java.time.format.DateTimeFormatter)
* @see java.time.MonthDay#parse(CharSequence, java.time.format.DateTimeFormatter)
*/
public final class TemporalAccessorParser implements Parser<TemporalAccessor> {

Expand Down Expand Up @@ -128,6 +133,12 @@ else if (OffsetDateTime.class == this.temporalAccessorType) {
else if (OffsetTime.class == this.temporalAccessorType) {
return OffsetTime.parse(text, formatterToUse);
}
else if (YearMonth.class == this.temporalAccessorType) {
return YearMonth.parse(text, formatterToUse);
}
else if (MonthDay.class == this.temporalAccessorType) {
return MonthDay.parse(text, formatterToUse);
}
else {
throw new IllegalStateException("Unsupported TemporalAccessor type: " + this.temporalAccessorType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @author Juergen Hoeller
* @author Phillip Webb
* @author Sam Brannen
* @author Kazuki Shimizu
*/
class DateTimeFormattingTests {

Expand Down Expand Up @@ -467,6 +468,16 @@ void testBindYearMonth() {
assertThat(binder.getBindingResult().getFieldValue("yearMonth").toString().equals("2007-12")).isTrue();
}

@Test
public void testBindYearMonthAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("yearMonthAnnotatedPattern", "12/2007");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("yearMonthAnnotatedPattern").toString().equals("12/2007"));
assertEquals(YearMonth.parse("2007-12"), binder.getBindingResult().getRawFieldValue("yearMonthAnnotatedPattern"));
}

@Test
void testBindMonthDay() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
Expand Down Expand Up @@ -557,6 +568,16 @@ void patternLocalDateWithUnsupportedPattern() {
}
}

@Test
public void testBindMonthDayAnnotatedPattern() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("monthDayAnnotatedPattern", "1/3");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("monthDayAnnotatedPattern").toString().equals("1/3"));
assertEquals(MonthDay.parse("--01-03"), binder.getBindingResult().getRawFieldValue("monthDayAnnotatedPattern"));
}


public static class DateTimeBean {

Expand Down Expand Up @@ -611,6 +632,12 @@ public static class DateTimeBean {

private YearMonth yearMonth;

@DateTimeFormat(pattern="MM/uuuu")
private YearMonth yearMonthAnnotatedPattern;

@DateTimeFormat(pattern="M/d")
private MonthDay monthDayAnnotatedPattern;

private MonthDay monthDay;

private final List<DateTimeBean> children = new ArrayList<>();
Expand Down Expand Up @@ -775,6 +802,14 @@ public void setYearMonth(YearMonth yearMonth) {
this.yearMonth = yearMonth;
}

public YearMonth getYearMonthAnnotatedPattern() {
return yearMonthAnnotatedPattern;
}

public void setYearMonthAnnotatedPattern(YearMonth yearMonthAnnotatedPattern) {
this.yearMonthAnnotatedPattern = yearMonthAnnotatedPattern;
}

public MonthDay getMonthDay() {
return this.monthDay;
}
Expand All @@ -783,6 +818,14 @@ public void setMonthDay(MonthDay monthDay) {
this.monthDay = monthDay;
}

public MonthDay getMonthDayAnnotatedPattern() {
return monthDayAnnotatedPattern;
}

public void setMonthDayAnnotatedPattern(MonthDay monthDayAnnotatedPattern) {
this.monthDayAnnotatedPattern = monthDayAnnotatedPattern;
}

public List<DateTimeBean> getChildren() {
return this.children;
}
Expand Down

0 comments on commit 65eceaf

Please sign in to comment.