@Test public void testBindLocalDateWithSpecificStyle() throws Exception { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setDateStyle(FormatStyle.LONG); setUp(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDate", "October 31, 2009"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("October 31, 2009", binder.getBindingResult().getFieldValue("localDate")); }
@Test public void testBindLocalTimeWithSpecificFormatter() throws Exception { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setTimeFormatter(DateTimeFormatter.ofPattern("HHmmss")); setUp(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "130000"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("130000", binder.getBindingResult().getFieldValue("localTime")); }
@Test public void testBindLocalTimeWithSpecificStyle() throws Exception { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setTimeStyle(FormatStyle.MEDIUM); setUp(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localTime", "12:00:00 PM"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTime")); }
@Test public void testBindDateTimeWithSpecificStyle() throws Exception { DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar(); registrar.setDateTimeStyle(FormatStyle.MEDIUM); setUp(registrar); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("localDateTime", LocalDateTime.of(2009, 10, 31, 12, 0)); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("localDateTime").toString(); assertTrue(value.startsWith("Oct 31, 2009")); assertTrue(value.endsWith("12:00:00 PM")); }
private void setUp(DateTimeFormatterRegistrar registrar) { conversionService = new FormattingConversionService(); DefaultConversionService.addDefaultConverters(conversionService); registrar.registerFormatters(conversionService); DateTimeBean bean = new DateTimeBean(); bean.getChildren().add(new DateTimeBean()); binder = new DataBinder(bean); binder.setConversionService(conversionService); LocaleContextHolder.setLocale(Locale.US); DateTimeContext context = new DateTimeContext(); context.setTimeZone(ZoneId.of("-05:00")); DateTimeContextHolder.setDateTimeContext(context); }