Skip to content

Commit 65eceaf

Browse files
kazuki43zoosnicoll
authored andcommitted
Add support for YearMonth and MonthDay in @DateTimeFormat
See gh-1215
1 parent a9d2016 commit 65eceaf

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

spring-context/src/main/java/org/springframework/format/datetime/standard/Jsr310DateTimeFormatAnnotationFormatterFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.time.LocalTime;
2222
import java.time.OffsetDateTime;
2323
import java.time.OffsetTime;
24+
import java.time.MonthDay;
25+
import java.time.YearMonth;
2426
import java.time.ZonedDateTime;
2527
import java.time.format.DateTimeFormatter;
2628
import java.time.temporal.TemporalAccessor;
@@ -43,6 +45,7 @@
4345
*
4446
* @author Juergen Hoeller
4547
* @author Sam Brannen
48+
* @author Kazuki Shimizu
4649
* @since 4.0
4750
* @see org.springframework.format.annotation.DateTimeFormat
4851
*/
@@ -60,6 +63,8 @@ public class Jsr310DateTimeFormatAnnotationFormatterFactory extends EmbeddedValu
6063
fieldTypes.add(ZonedDateTime.class);
6164
fieldTypes.add(OffsetDateTime.class);
6265
fieldTypes.add(OffsetTime.class);
66+
fieldTypes.add(YearMonth.class);
67+
fieldTypes.add(MonthDay.class);
6368
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
6469
}
6570

spring-context/src/main/java/org/springframework/format/datetime/standard/TemporalAccessorParser.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import java.time.LocalDate;
2121
import java.time.LocalDateTime;
2222
import java.time.LocalTime;
23+
import java.time.MonthDay;
2324
import java.time.OffsetDateTime;
2425
import java.time.OffsetTime;
26+
import java.time.YearMonth;
2527
import java.time.ZonedDateTime;
2628
import java.time.format.DateTimeFormatter;
2729
import java.time.format.DateTimeParseException;
@@ -38,6 +40,7 @@
3840
*
3941
* @author Juergen Hoeller
4042
* @author Sam Brannen
43+
* @author Kazuki Shimizu
4144
* @since 4.0
4245
* @see DateTimeContextHolder#getFormatter
4346
* @see java.time.LocalDate#parse(CharSequence, java.time.format.DateTimeFormatter)
@@ -46,6 +49,8 @@
4649
* @see java.time.ZonedDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
4750
* @see java.time.OffsetDateTime#parse(CharSequence, java.time.format.DateTimeFormatter)
4851
* @see java.time.OffsetTime#parse(CharSequence, java.time.format.DateTimeFormatter)
52+
* @see java.time.YearMonth#parse(CharSequence, java.time.format.DateTimeFormatter)
53+
* @see java.time.MonthDay#parse(CharSequence, java.time.format.DateTimeFormatter)
4954
*/
5055
public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
5156

@@ -128,6 +133,12 @@ else if (OffsetDateTime.class == this.temporalAccessorType) {
128133
else if (OffsetTime.class == this.temporalAccessorType) {
129134
return OffsetTime.parse(text, formatterToUse);
130135
}
136+
else if (YearMonth.class == this.temporalAccessorType) {
137+
return YearMonth.parse(text, formatterToUse);
138+
}
139+
else if (MonthDay.class == this.temporalAccessorType) {
140+
return MonthDay.parse(text, formatterToUse);
141+
}
131142
else {
132143
throw new IllegalStateException("Unsupported TemporalAccessor type: " + this.temporalAccessorType);
133144
}

spring-context/src/test/java/org/springframework/format/datetime/standard/DateTimeFormattingTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author Juergen Hoeller
6565
* @author Phillip Webb
6666
* @author Sam Brannen
67+
* @author Kazuki Shimizu
6768
*/
6869
class DateTimeFormattingTests {
6970

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

471+
@Test
472+
public void testBindYearMonthAnnotatedPattern() {
473+
MutablePropertyValues propertyValues = new MutablePropertyValues();
474+
propertyValues.add("yearMonthAnnotatedPattern", "12/2007");
475+
binder.bind(propertyValues);
476+
assertEquals(0, binder.getBindingResult().getErrorCount());
477+
assertTrue(binder.getBindingResult().getFieldValue("yearMonthAnnotatedPattern").toString().equals("12/2007"));
478+
assertEquals(YearMonth.parse("2007-12"), binder.getBindingResult().getRawFieldValue("yearMonthAnnotatedPattern"));
479+
}
480+
470481
@Test
471482
void testBindMonthDay() {
472483
MutablePropertyValues propertyValues = new MutablePropertyValues();
@@ -557,6 +568,16 @@ void patternLocalDateWithUnsupportedPattern() {
557568
}
558569
}
559570

571+
@Test
572+
public void testBindMonthDayAnnotatedPattern() {
573+
MutablePropertyValues propertyValues = new MutablePropertyValues();
574+
propertyValues.add("monthDayAnnotatedPattern", "1/3");
575+
binder.bind(propertyValues);
576+
assertEquals(0, binder.getBindingResult().getErrorCount());
577+
assertTrue(binder.getBindingResult().getFieldValue("monthDayAnnotatedPattern").toString().equals("1/3"));
578+
assertEquals(MonthDay.parse("--01-03"), binder.getBindingResult().getRawFieldValue("monthDayAnnotatedPattern"));
579+
}
580+
560581

561582
public static class DateTimeBean {
562583

@@ -611,6 +632,12 @@ public static class DateTimeBean {
611632

612633
private YearMonth yearMonth;
613634

635+
@DateTimeFormat(pattern="MM/uuuu")
636+
private YearMonth yearMonthAnnotatedPattern;
637+
638+
@DateTimeFormat(pattern="M/d")
639+
private MonthDay monthDayAnnotatedPattern;
640+
614641
private MonthDay monthDay;
615642

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

805+
public YearMonth getYearMonthAnnotatedPattern() {
806+
return yearMonthAnnotatedPattern;
807+
}
808+
809+
public void setYearMonthAnnotatedPattern(YearMonth yearMonthAnnotatedPattern) {
810+
this.yearMonthAnnotatedPattern = yearMonthAnnotatedPattern;
811+
}
812+
778813
public MonthDay getMonthDay() {
779814
return this.monthDay;
780815
}
@@ -783,6 +818,14 @@ public void setMonthDay(MonthDay monthDay) {
783818
this.monthDay = monthDay;
784819
}
785820

821+
public MonthDay getMonthDayAnnotatedPattern() {
822+
return monthDayAnnotatedPattern;
823+
}
824+
825+
public void setMonthDayAnnotatedPattern(MonthDay monthDayAnnotatedPattern) {
826+
this.monthDayAnnotatedPattern = monthDayAnnotatedPattern;
827+
}
828+
786829
public List<DateTimeBean> getChildren() {
787830
return this.children;
788831
}

0 commit comments

Comments
 (0)