Skip to content

Commit 8144f86

Browse files
committed
Merge pull request #1215 from kazuki43zoo
* pr/1215: Polish "Add support for YearMonth and MonthDay in @DateTimeFormat" Add support for YearMonth and MonthDay in @DateTimeFormat Closes gh-1215
2 parents a9d2016 + a57ea39 commit 8144f86

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import java.time.LocalDate;
2020
import java.time.LocalDateTime;
2121
import java.time.LocalTime;
22+
import java.time.MonthDay;
2223
import java.time.OffsetDateTime;
2324
import java.time.OffsetTime;
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,10 +63,11 @@ 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

66-
6771
@Override
6872
public final Set<Class<?>> getFieldTypes() {
6973
return FIELD_TYPES;

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+
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
477+
assertThat(binder.getBindingResult().getFieldValue("yearMonthAnnotatedPattern")).isEqualTo("12/2007");
478+
assertThat(binder.getBindingResult().getRawFieldValue("yearMonthAnnotatedPattern")).isEqualTo(YearMonth.parse("2007-12"));
479+
}
480+
470481
@Test
471482
void testBindMonthDay() {
472483
MutablePropertyValues propertyValues = new MutablePropertyValues();
@@ -476,6 +487,16 @@ void testBindMonthDay() {
476487
assertThat(binder.getBindingResult().getFieldValue("monthDay").toString().equals("--12-03")).isTrue();
477488
}
478489

490+
@Test
491+
public void testBindMonthDayAnnotatedPattern() {
492+
MutablePropertyValues propertyValues = new MutablePropertyValues();
493+
propertyValues.add("monthDayAnnotatedPattern", "1/3");
494+
binder.bind(propertyValues);
495+
assertThat(binder.getBindingResult().getErrorCount()).isEqualTo(0);
496+
assertThat(binder.getBindingResult().getFieldValue("monthDayAnnotatedPattern")).isEqualTo("1/3");
497+
assertThat(binder.getBindingResult().getRawFieldValue("monthDayAnnotatedPattern")).isEqualTo(MonthDay.parse("--01-03"));
498+
}
499+
479500
@Nested
480501
class FallbackPatternTests {
481502

@@ -611,8 +632,14 @@ public static class DateTimeBean {
611632

612633
private YearMonth yearMonth;
613634

635+
@DateTimeFormat(pattern="MM/uuuu")
636+
private YearMonth yearMonthAnnotatedPattern;
637+
614638
private MonthDay monthDay;
615639

640+
@DateTimeFormat(pattern="M/d")
641+
private MonthDay monthDayAnnotatedPattern;
642+
616643
private final List<DateTimeBean> children = new ArrayList<>();
617644

618645

@@ -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)