Add support for YearMonth and MonthDay in @DateTimeFormat

See gh-1215
This commit is contained in:
Kazuki Shimizu
2016-10-22 16:27:04 +09:00
committed by Stephane Nicoll
parent a9d2016007
commit 65eceafeee
3 changed files with 59 additions and 0 deletions

View File

@@ -21,6 +21,8 @@ import java.time.LocalDateTime;
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;
@@ -43,6 +45,7 @@ import org.springframework.util.StringUtils;
*
* @author Juergen Hoeller
* @author Sam Brannen
* @author Kazuki Shimizu
* @since 4.0
* @see org.springframework.format.annotation.DateTimeFormat
*/
@@ -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);
}

View File

@@ -20,8 +20,10 @@ import java.text.ParseException;
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;
@@ -38,6 +40,7 @@ import org.springframework.util.ObjectUtils;
*
* @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)
@@ -46,6 +49,8 @@ import org.springframework.util.ObjectUtils;
* @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> {
@@ -128,6 +133,12 @@ public final class TemporalAccessorParser implements Parser<TemporalAccessor> {
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);
}