Formatting support for java.time.Year and java.time.Month

Issue: SPR-16437
This commit is contained in:
Juergen Hoeller
2018-01-30 15:54:47 +01:00
parent 89d2bd954a
commit ef2e16912d
4 changed files with 162 additions and 19 deletions

View File

@@ -21,8 +21,10 @@ import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@@ -60,12 +62,12 @@ public class DateTimeFormattingTests {
@Before
public void setUp() {
public void setup() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
setUp(registrar);
setup(registrar);
}
private void setUp(DateTimeFormatterRegistrar registrar) {
private void setup(DateTimeFormatterRegistrar registrar) {
conversionService = new FormattingConversionService();
DefaultConversionService.addDefaultConverters(conversionService);
registrar.registerFormatters(conversionService);
@@ -82,7 +84,7 @@ public class DateTimeFormattingTests {
}
@After
public void tearDown() {
public void cleanup() {
LocaleContextHolder.setLocale(null);
DateTimeContextHolder.setDateTimeContext(null);
}
@@ -98,10 +100,10 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalDateWithSpecificStyle() throws Exception {
public void testBindLocalDateWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateStyle(FormatStyle.LONG);
setUp(registrar);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "October 31, 2009");
binder.bind(propertyValues);
@@ -110,10 +112,10 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalDateWithSpecificFormatter() throws Exception {
public void testBindLocalDateWithSpecificFormatter() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyyMMdd"));
setUp(registrar);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", "20091031");
binder.bind(propertyValues);
@@ -177,7 +179,7 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalDateFromJavaUtilCalendar() throws Exception {
public void testBindLocalDateFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDate", new GregorianCalendar(2009, 9, 31, 0, 0));
binder.bind(propertyValues);
@@ -195,10 +197,10 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalTimeWithSpecificStyle() throws Exception {
public void testBindLocalTimeWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeStyle(FormatStyle.MEDIUM);
setUp(registrar);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "12:00:00 PM");
binder.bind(propertyValues);
@@ -207,10 +209,10 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalTimeWithSpecificFormatter() throws Exception {
public void testBindLocalTimeWithSpecificFormatter() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss"));
setUp(registrar);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", "130000");
binder.bind(propertyValues);
@@ -228,7 +230,7 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalTimeFromJavaUtilCalendar() throws Exception {
public void testBindLocalTimeFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localTime", new GregorianCalendar(1970, 0, 0, 12, 0));
binder.bind(propertyValues);
@@ -259,7 +261,7 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindLocalDateTimeFromJavaUtilCalendar() throws Exception {
public void testBindLocalDateTimeFromJavaUtilCalendar() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", new GregorianCalendar(2009, 9, 31, 12, 0));
binder.bind(propertyValues);
@@ -270,10 +272,10 @@ public class DateTimeFormattingTests {
}
@Test
public void testBindDateTimeWithSpecificStyle() throws Exception {
public void testBindDateTimeWithSpecificStyle() {
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateTimeStyle(FormatStyle.MEDIUM);
setUp(registrar);
setup(registrar);
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0));
binder.bind(propertyValues);
@@ -356,7 +358,7 @@ public class DateTimeFormattingTests {
@Test
@SuppressWarnings("deprecation")
public void testBindInstantFromJavaUtilDate() throws Exception {
public void testBindInstantFromJavaUtilDate() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("instant", new Date(109, 9, 31, 12, 0));
binder.bind(propertyValues);
@@ -382,6 +384,33 @@ public class DateTimeFormattingTests {
assertTrue(binder.getBindingResult().getFieldValue("duration").toString().equals("PT8H6M12.345S"));
}
@Test
public void testBindYear() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("year", "2007");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("year").toString().equals("2007"));
}
@Test
public void testBindMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("month", "JULY");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("month").toString().equals("JULY"));
}
@Test
public void testBindMonthInAnyCase() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
propertyValues.add("month", "July");
binder.bind(propertyValues);
assertEquals(0, binder.getBindingResult().getErrorCount());
assertTrue(binder.getBindingResult().getFieldValue("month").toString().equals("JULY"));
}
@Test
public void testBindYearMonth() {
MutablePropertyValues propertyValues = new MutablePropertyValues();
@@ -436,6 +465,10 @@ public class DateTimeFormattingTests {
private Duration duration;
private Year year;
private Month month;
private YearMonth yearMonth;
private MonthDay monthDay;
@@ -546,6 +579,22 @@ public class DateTimeFormattingTests {
this.duration = duration;
}
public Year getYear() {
return year;
}
public void setYear(Year year) {
this.year = year;
}
public Month getMonth() {
return month;
}
public void setMonth(Month month) {
this.month = month;
}
public YearMonth getYearMonth() {
return yearMonth;
}