added support for Instant and MutableDateTime binding to JodaTime formatting system; allow for use of @DateTimeFormat on any ReadableInstant field

This commit is contained in:
Keith Donald
2011-02-04 20:17:52 +00:00
parent ff7f3aebea
commit fbb1fa33a1
4 changed files with 123 additions and 14 deletions

View File

@@ -22,7 +22,6 @@ import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
@@ -30,7 +29,6 @@ import org.joda.time.LocalTime;
import org.joda.time.ReadableInstant;
import org.joda.time.ReadablePartial;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Parser;
@@ -57,16 +55,7 @@ public class JodaDateTimeFormatAnnotationFormatterFactory
public JodaDateTimeFormatAnnotationFormatterFactory() {
Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(8);
rawFieldTypes.add(LocalDate.class);
rawFieldTypes.add(LocalTime.class);
rawFieldTypes.add(LocalDateTime.class);
rawFieldTypes.add(DateTime.class);
rawFieldTypes.add(DateMidnight.class);
rawFieldTypes.add(Date.class);
rawFieldTypes.add(Calendar.class);
rawFieldTypes.add(Long.class);
this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes);
this.fieldTypes = createFieldTypes();
}
public final Set<Class<?>> getFieldTypes() {
@@ -105,7 +94,26 @@ public class JodaDateTimeFormatAnnotationFormatterFactory
return new DateTimeParser(configureDateTimeFormatterFrom(annotation));
}
// internal helpers
/**
* Create the set of field types that may be annotated with @DateTimeFormat.
* Note: the 3 ReadablePartial concrete types are registered explicitly since addFormatterForFieldType rules exist for each of these types
* (if we did not do this, the default byType rules for LocalDate, LocalTime, and LocalDateTime would take precedence over the annotation rule, which is not what we want)
* @see JodaTimeFormatterRegistrar#registerFormatters(org.springframework.format.FormatterRegistry)
*/
private Set<Class<?>> createFieldTypes() {
Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(7);
rawFieldTypes.add(ReadableInstant.class);
rawFieldTypes.add(LocalDate.class);
rawFieldTypes.add(LocalTime.class);
rawFieldTypes.add(LocalDateTime.class);
rawFieldTypes.add(Date.class);
rawFieldTypes.add(Calendar.class);
rawFieldTypes.add(Long.class);
return Collections.unmodifiableSet(rawFieldTypes);
}
private DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) {
if (StringUtils.hasLength(annotation.pattern())) {
return forPattern(resolveEmbeddedValue(annotation.pattern()));

View File

@@ -21,11 +21,12 @@ import java.util.Date;
import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Instant;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.MutableDateTime;
import org.joda.time.ReadableInstant;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
@@ -46,6 +47,8 @@ final class JodaTimeConverters {
registry.addConverter(new DateTimeToLocalTimeConverter());
registry.addConverter(new DateTimeToLocalDateTimeConverter());
registry.addConverter(new DateTimeToDateMidnightConverter());
registry.addConverter(new DateTimeToInstantConverter());
registry.addConverter(new DateTimeToMutableDateTimeConverter());
registry.addConverter(new DateTimeToDateConverter());
registry.addConverter(new DateTimeToCalendarConverter());
registry.addConverter(new DateTimeToLongConverter());
@@ -94,6 +97,26 @@ final class JodaTimeConverters {
}
}
/**
* Used when binding a parsed DateTime to an Instant field.
* @see DateTimeParser
*/
private static class DateTimeToInstantConverter implements Converter<DateTime, Instant> {
public Instant convert(DateTime source) {
return source.toInstant();
}
}
/**
* Used when binding a parsed DateTime to a MutableDateTime field.
* @see DateTimeParser
*/
private static class DateTimeToMutableDateTimeConverter implements Converter<DateTime, MutableDateTime> {
public MutableDateTime convert(DateTime source) {
return source.toMutableDateTime();
}
}
/**
* Used when binding a parsed DateTime to a java.util.Date field.
* @see DateTimeParser

View File

@@ -117,7 +117,6 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
}
if (this.dateStyle != null) {
return DateTimeFormat.forStyle(this.dateStyle + "-");
} else {
return DateTimeFormat.shortDate();
}