diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/AbstractDateTimeAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/AbstractDateTimeAnnotationFormatterFactory.java
index 8c8b4fe06ab..d2805ab59d4 100644
--- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/AbstractDateTimeAnnotationFormatterFactory.java
+++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/AbstractDateTimeAnnotationFormatterFactory.java
@@ -41,23 +41,23 @@ import org.springframework.ui.format.Printer;
*/
abstract class AbstractDateTimeAnnotationFormatterFactory implements AnnotationFormatterFactory {
- private final Set> propertyTypes;
+ private final Set> fieldTypes;
public AbstractDateTimeAnnotationFormatterFactory() {
- this.propertyTypes = Collections.unmodifiableSet(createFieldTypes());
+ this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
}
public Set> getFieldTypes() {
- return propertyTypes;
+ return this.fieldTypes;
}
- public Printer> getPrinter(A annotation, Class> propertyType) {
+ public Printer> getPrinter(A annotation, Class> fieldType) {
DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation);
- if (ReadableInstant.class.isAssignableFrom(propertyType)) {
+ if (ReadableInstant.class.isAssignableFrom(fieldType)) {
return new ReadableInstantPrinter(formatter);
- } else if (ReadablePartial.class.isAssignableFrom(propertyType)) {
+ } else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
return new ReadablePartialPrinter(formatter);
- } else if (Calendar.class.isAssignableFrom(propertyType)) {
+ } else if (Calendar.class.isAssignableFrom(fieldType)) {
// assumes Calendar->ReadableInstant converter is registered
return new ReadableInstantPrinter(formatter);
} else {
@@ -80,15 +80,15 @@ abstract class AbstractDateTimeAnnotationFormatterFactory
// internal helpers
private Set> createFieldTypes() {
- Set> propertyTypes = new HashSet>(5);
- propertyTypes.add(LocalDate.class);
- propertyTypes.add(LocalTime.class);
- propertyTypes.add(LocalDateTime.class);
- propertyTypes.add(DateTime.class);
- propertyTypes.add(Date.class);
- propertyTypes.add(Calendar.class);
- propertyTypes.add(Long.class);
- return propertyTypes;
+ Set> fieldTypes = new HashSet>(7);
+ fieldTypes.add(LocalDate.class);
+ fieldTypes.add(LocalTime.class);
+ fieldTypes.add(LocalDateTime.class);
+ fieldTypes.add(DateTime.class);
+ fieldTypes.add(Date.class);
+ fieldTypes.add(Calendar.class);
+ fieldTypes.add(Long.class);
+ return fieldTypes;
}
}
diff --git a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeConverters.java b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeConverters.java
index 9cb55ae822c..e08ab338506 100644
--- a/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeConverters.java
+++ b/org.springframework.context/src/main/java/org/springframework/ui/format/jodatime/JodaTimeConverters.java
@@ -48,6 +48,7 @@ final class JodaTimeConverters {
registry.addConverter(new DateTimeToDateMidnightConverter());
registry.addConverter(new DateTimeToDateConverter());
registry.addConverter(new DateTimeToCalendarConverter());
+ registry.addConverter(new DateTimeToLongConverter());
registry.addConverter(new DateToLongConverter());
registry.addConverter(new CalendarToReadableInstantConverter());
}
@@ -114,6 +115,16 @@ final class JodaTimeConverters {
}
}
+ /**
+ * Used when binding a parsed DateTime to a java.lang.Long field.
+ * @see DateTimeParser
+ */
+ private static class DateTimeToLongConverter implements Converter {
+ public Long convert(DateTime source) {
+ return source.getMillis();
+ }
+ }
+
/**
* Used when printing a java.util.Date field with a MillisecondInstantPrinter.
* @see MillisecondInstantPrinter
diff --git a/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java b/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java
index 4b564c01a34..b6651855610 100644
--- a/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java
+++ b/org.springframework.context/src/test/java/org/springframework/ui/format/jodatime/JodaTimeFormattingTests.java
@@ -12,6 +12,7 @@ import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.i18n.LocaleContextHolder;
@@ -29,7 +30,7 @@ public class JodaTimeFormattingTests {
public void setUp() {
JodaTimeFormattingConfigurer configurer = new JodaTimeFormattingConfigurer();
configurer.installJodaTimeFormatting(conversionService);
-
+
binder = new DataBinder(new JodaTimeBean());
binder.setConversionService(conversionService);
@@ -66,6 +67,117 @@ public class JodaTimeFormattingTests {
assertEquals(0, binder.getBindingResult().getErrorCount());
assertEquals("Oct 31, 2009", binder.getBindingResult().getFieldValue("localDateAnnotated"));
}
+
+ @Test
+ public void testBindLocalTime() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("localTime", "12:00 PM");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("12:00 PM", binder.getBindingResult().getFieldValue("localTime"));
+ }
+
+ @Test
+ public void testBindLocalTimeAnnotated() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("localTimeAnnotated", "12:00:00 PM");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("12:00:00 PM", binder.getBindingResult().getFieldValue("localTimeAnnotated"));
+ }
+
+ @Test
+ public void testBindLocalDateTime() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("localDateTime", "10/31/09 12:00 PM");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("localDateTime"));
+ }
+
+ @Test
+ public void testBindLocalDateTimeAnnotated() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("localDateTimeAnnotated", "Saturday, October 31, 2009 12:00:00 PM ");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("Saturday, October 31, 2009 12:00:00 PM ", binder.getBindingResult().getFieldValue("localDateTimeAnnotated"));
+ }
+
+ @Test
+ @Ignore
+ public void testBindDateTime() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("dateTime", "10/31/09 12:00 PM");
+ binder.bind(propertyValues);
+ System.out.println(binder.getBindingResult());
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTime"));
+ }
+
+ @Test
+ public void testBindDateTimeAnnotated() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("dateTimeAnnotated", "Oct 31, 2009 12:00 PM");
+ binder.bind(propertyValues);
+ System.out.println(binder.getBindingResult());
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated"));
+ }
+
+ @Test
+ public void testBindDate() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("date", "10/31/09 12:00 PM");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("date"));
+ }
+
+ @Test
+ public void testBindDateAnnotated() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("dateAnnotated", "10/31/09");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated"));
+ }
+
+ @Test
+ public void testBindCalendar() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("calendar", "10/31/09 12:00 PM");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("calendar"));
+ }
+
+ @Test
+ public void testBindCalendarAnnotated() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("calendarAnnotated", "10/31/09");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09", binder.getBindingResult().getFieldValue("calendarAnnotated"));
+ }
+
+ @Test
+ public void testBindLong() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("millis", "1256961600");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("1256961600", binder.getBindingResult().getFieldValue("millis"));
+ }
+
+ @Test
+ public void testBindLongAnnotated() {
+ MutablePropertyValues propertyValues = new MutablePropertyValues();
+ propertyValues.addPropertyValue("millisAnnotated", "10/31/09");
+ binder.bind(propertyValues);
+ assertEquals(0, binder.getBindingResult().getErrorCount());
+ assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated"));
+ }
public static class JodaTimeBean {
@@ -76,8 +188,8 @@ public class JodaTimeFormattingTests {
private LocalTime localTime;
- @DateTimeFormat(timeStyle = Style.LONG)
- private LocalDate localTimeAnnotated;
+ @DateTimeFormat(timeStyle = Style.MEDIUM)
+ private LocalTime localTimeAnnotated;
private LocalDateTime localDateTime;
@@ -87,7 +199,7 @@ public class JodaTimeFormattingTests {
private DateTime dateTime;
@DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT)
- private LocalDateTime dateTimeAnnotated;
+ private DateTime dateTimeAnnotated;
private Date date;
@@ -128,11 +240,11 @@ public class JodaTimeFormattingTests {
this.localTime = localTime;
}
- public LocalDate getLocalTimeAnnotated() {
+ public LocalTime getLocalTimeAnnotated() {
return localTimeAnnotated;
}
- public void setLocalTimeAnnotated(LocalDate localTimeAnnotated) {
+ public void setLocalTimeAnnotated(LocalTime localTimeAnnotated) {
this.localTimeAnnotated = localTimeAnnotated;
}
@@ -160,11 +272,11 @@ public class JodaTimeFormattingTests {
this.dateTime = dateTime;
}
- public LocalDateTime getDateTimeAnnotated() {
+ public DateTime getDateTimeAnnotated() {
return dateTimeAnnotated;
}
- public void setDateTimeAnnotated(LocalDateTime dateTimeAnnotated) {
+ public void setDateTimeAnnotated(DateTime dateTimeAnnotated) {
this.dateTimeAnnotated = dateTimeAnnotated;
}