Browse Source
Dependency on Joda Time when using the @DateTimeFormat annotation is now optional. If Joda Time is not present the JDK SimpleDateFormat will be used to parse and print date patterns. If Joda time is present it will always be used in preference to SimpleDateFormat. Issue: SPR-6508pull/161/head
9 changed files with 806 additions and 85 deletions
@ -0,0 +1,123 @@
@@ -0,0 +1,123 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.format.datetime; |
||||
|
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
|
||||
import org.springframework.core.convert.converter.Converter; |
||||
import org.springframework.core.convert.converter.ConverterRegistry; |
||||
import org.springframework.format.FormatterRegistrar; |
||||
import org.springframework.format.FormatterRegistry; |
||||
import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Configures Date formatting for use with Spring. |
||||
* <p> |
||||
* Designed for direct instantiation but also exposes the static |
||||
* {@link #addDateConverters(ConverterRegistry)} utility method for ad hoc use against any |
||||
* {@code ConverterRegistry} instance. |
||||
* |
||||
* @author Phillip Webb |
||||
* @since 3.2 |
||||
* @see JodaTimeFormatterRegistrar |
||||
* @see FormatterRegistrar#registerFormatters |
||||
*/ |
||||
public class DateFormatterRegistrar implements FormatterRegistrar { |
||||
|
||||
|
||||
private DateFormatter dateFormatter = new DateFormatter(); |
||||
|
||||
|
||||
public void registerFormatters(FormatterRegistry registry) { |
||||
addDateConverters(registry); |
||||
registry.addFormatter(dateFormatter); |
||||
registry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); |
||||
} |
||||
|
||||
/** |
||||
* Set the date formatter to register. If not specified default {@link DateFormatter} |
||||
* will be used. This method can be used if additional formatter configuration is |
||||
* required. |
||||
* @param dateFormatter the date formatter |
||||
*/ |
||||
public void setFormatter(DateFormatter dateFormatter) { |
||||
Assert.notNull(dateFormatter,"DateFormatter must not be null"); |
||||
this.dateFormatter = dateFormatter; |
||||
} |
||||
|
||||
/** |
||||
* Add date converters to the specified registry. |
||||
* @param converterRegistry the registry of converters to add to |
||||
*/ |
||||
public static void addDateConverters(ConverterRegistry converterRegistry) { |
||||
converterRegistry.addConverter(new DateToLongConverter()); |
||||
converterRegistry.addConverter(new DateToCalendarConverter()); |
||||
converterRegistry.addConverter(new CalendarToDateConverter()); |
||||
converterRegistry.addConverter(new CalendarToLongConverter()); |
||||
converterRegistry.addConverter(new LongToDateConverter()); |
||||
converterRegistry.addConverter(new LongToCalendarConverter()); |
||||
} |
||||
|
||||
|
||||
private static class DateToLongConverter implements Converter<Date, Long> { |
||||
public Long convert(Date source) { |
||||
return source.getTime(); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class DateToCalendarConverter implements Converter<Date, Calendar> { |
||||
public Calendar convert(Date source) { |
||||
Calendar calendar = Calendar.getInstance(); |
||||
calendar.setTime(source); |
||||
return calendar; |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class CalendarToDateConverter implements Converter<Calendar, Date> { |
||||
public Date convert(Calendar source) { |
||||
return source.getTime(); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class CalendarToLongConverter implements Converter<Calendar, Long> { |
||||
public Long convert(Calendar source) { |
||||
return source.getTime().getTime(); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class LongToDateConverter implements Converter<Long, Date> { |
||||
public Date convert(Long source) { |
||||
return new Date(source); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class LongToCalendarConverter implements Converter<Long, Calendar> { |
||||
|
||||
private DateToCalendarConverter dateToCalendarConverter = new DateToCalendarConverter(); |
||||
|
||||
public Calendar convert(Long source) { |
||||
return dateToCalendarConverter.convert(new Date(source)); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.format.datetime; |
||||
|
||||
import java.util.Calendar; |
||||
import java.util.Collections; |
||||
import java.util.Date; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.context.EmbeddedValueResolverAware; |
||||
import org.springframework.format.AnnotationFormatterFactory; |
||||
import org.springframework.format.Formatter; |
||||
import org.springframework.format.Parser; |
||||
import org.springframework.format.Printer; |
||||
import org.springframework.format.annotation.DateTimeFormat; |
||||
import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory; |
||||
import org.springframework.util.StringValueResolver; |
||||
|
||||
/** |
||||
* Formats fields annotated with the {@link DateTimeFormat} annotation. |
||||
* |
||||
* @author Phillip Webb |
||||
* @see JodaDateTimeFormatAnnotationFormatterFactory |
||||
* @since 3.2 |
||||
*/ |
||||
public class DateTimeFormatAnnotationFormatterFactory implements |
||||
AnnotationFormatterFactory<DateTimeFormat>, EmbeddedValueResolverAware { |
||||
|
||||
|
||||
private static final Set<Class<?>> FIELD_TYPES; |
||||
static { |
||||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(); |
||||
fieldTypes.add(Date.class); |
||||
fieldTypes.add(Calendar.class); |
||||
fieldTypes.add(Long.class); |
||||
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes); |
||||
} |
||||
|
||||
|
||||
private StringValueResolver embeddedValueResolver; |
||||
|
||||
|
||||
public void setEmbeddedValueResolver(StringValueResolver resolver) { |
||||
this.embeddedValueResolver = resolver; |
||||
} |
||||
|
||||
public Set<Class<?>> getFieldTypes() { |
||||
return FIELD_TYPES; |
||||
} |
||||
|
||||
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) { |
||||
return getFormatter(annotation, fieldType); |
||||
} |
||||
|
||||
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) { |
||||
return getFormatter(annotation, fieldType); |
||||
} |
||||
|
||||
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) { |
||||
DateFormatter formatter = new DateFormatter(); |
||||
formatter.setStylePattern(resolveEmbeddedValue(annotation.style())); |
||||
formatter.setIso(annotation.iso()); |
||||
formatter.setPattern(resolveEmbeddedValue(annotation.pattern())); |
||||
return formatter; |
||||
} |
||||
|
||||
protected String resolveEmbeddedValue(String value) { |
||||
return (this.embeddedValueResolver != null ? this.embeddedValueResolver.resolveStringValue(value) : value); |
||||
} |
||||
} |
||||
@ -0,0 +1,300 @@
@@ -0,0 +1,300 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.format.datetime; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
import java.util.Locale; |
||||
|
||||
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; |
||||
import org.springframework.core.convert.support.DefaultConversionService; |
||||
import org.springframework.format.annotation.DateTimeFormat; |
||||
import org.springframework.format.annotation.DateTimeFormat.ISO; |
||||
import org.springframework.format.datetime.DateFormatterRegistrar; |
||||
import org.springframework.format.support.FormattingConversionService; |
||||
import org.springframework.validation.DataBinder; |
||||
|
||||
/** |
||||
* @author Phillip Webb |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
*/ |
||||
public class DateFormattingTests { |
||||
|
||||
private FormattingConversionService conversionService = new FormattingConversionService(); |
||||
|
||||
private DataBinder binder; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
DefaultConversionService.addDefaultConverters(conversionService); |
||||
|
||||
DateFormatterRegistrar registrar = new DateFormatterRegistrar(); |
||||
registrar.registerFormatters(conversionService); |
||||
|
||||
SimpleDateBean bean = new SimpleDateBean(); |
||||
bean.getChildren().add(new SimpleDateBean()); |
||||
binder = new DataBinder(bean); |
||||
binder.setConversionService(conversionService); |
||||
|
||||
LocaleContextHolder.setLocale(Locale.US); |
||||
} |
||||
|
||||
@After |
||||
public void tearDown() { |
||||
LocaleContextHolder.setLocale(null); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindDate() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("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 testBindDateArray() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("date", new String[] {"10/31/09 12:00 PM"}); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindDateAnnotated() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("dateAnnotated", "10/31/09"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("dateAnnotated")); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindDateAnnotatedWithError() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("dateAnnotated", "Oct X31, 2009"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(1, binder.getBindingResult().getFieldErrorCount("dateAnnotated")); |
||||
assertEquals("Oct X31, 2009", binder.getBindingResult().getFieldValue("dateAnnotated")); |
||||
} |
||||
|
||||
@Test |
||||
@Ignore |
||||
public void testBindDateAnnotatedWithFallbackError() { |
||||
// TODO This currently passes because of the Date(String) constructor fallback is used
|
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("dateAnnotated", "Oct 031, 2009"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(1, binder.getBindingResult().getFieldErrorCount("dateAnnotated")); |
||||
assertEquals("Oct 031, 2009", binder.getBindingResult().getFieldValue("dateAnnotated")); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindCalendar() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("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.add("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.add("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.add("millisAnnotated", "10/31/09"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("millisAnnotated")); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindISODate() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("isoDate", "2009-10-31"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
assertEquals("2009-10-31", binder.getBindingResult().getFieldValue("isoDate")); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindISOTime() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("isoTime", "12:00:00.000-0500"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
assertEquals("17:00:00.000+0000", binder.getBindingResult().getFieldValue("isoTime")); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindISODateTime() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("isoDateTime", "2009-10-31T12:00:00.000-0800"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
assertEquals("2009-10-31T20:00:00.000+0000", binder.getBindingResult().getFieldValue("isoDateTime")); |
||||
} |
||||
|
||||
@Test |
||||
public void testBindNestedDateAnnotated() { |
||||
MutablePropertyValues propertyValues = new MutablePropertyValues(); |
||||
propertyValues.add("children[0].dateAnnotated", "10/31/09"); |
||||
binder.bind(propertyValues); |
||||
assertEquals(0, binder.getBindingResult().getErrorCount()); |
||||
assertEquals("10/31/09", binder.getBindingResult().getFieldValue("children[0].dateAnnotated")); |
||||
} |
||||
|
||||
@SuppressWarnings("unused") |
||||
private static class SimpleDateBean { |
||||
|
||||
@DateTimeFormat |
||||
private Date date; |
||||
|
||||
@DateTimeFormat(style="S-") |
||||
private Date dateAnnotated; |
||||
|
||||
@DateTimeFormat |
||||
private Calendar calendar; |
||||
|
||||
@DateTimeFormat(style="S-") |
||||
private Calendar calendarAnnotated; |
||||
|
||||
private Long millis; |
||||
|
||||
private Long millisAnnotated; |
||||
|
||||
@DateTimeFormat(pattern="M/d/yy h:mm a") |
||||
private Date dateAnnotatedPattern; |
||||
|
||||
@DateTimeFormat(iso=ISO.DATE) |
||||
private Date isoDate; |
||||
|
||||
@DateTimeFormat(iso=ISO.TIME) |
||||
private Date isoTime; |
||||
|
||||
@DateTimeFormat(iso=ISO.DATE_TIME) |
||||
private Date isoDateTime; |
||||
|
||||
private final List<SimpleDateBean> children = new ArrayList<SimpleDateBean>(); |
||||
|
||||
public Date getDate() { |
||||
return date; |
||||
} |
||||
|
||||
public void setDate(Date date) { |
||||
this.date = date; |
||||
} |
||||
|
||||
public Date getDateAnnotated() { |
||||
return dateAnnotated; |
||||
} |
||||
|
||||
public void setDateAnnotated(Date dateAnnotated) { |
||||
this.dateAnnotated = dateAnnotated; |
||||
} |
||||
|
||||
public Calendar getCalendar() { |
||||
return calendar; |
||||
} |
||||
|
||||
public void setCalendar(Calendar calendar) { |
||||
this.calendar = calendar; |
||||
} |
||||
|
||||
public Calendar getCalendarAnnotated() { |
||||
return calendarAnnotated; |
||||
} |
||||
|
||||
public void setCalendarAnnotated(Calendar calendarAnnotated) { |
||||
this.calendarAnnotated = calendarAnnotated; |
||||
} |
||||
|
||||
public Long getMillis() { |
||||
return millis; |
||||
} |
||||
|
||||
public void setMillis(Long millis) { |
||||
this.millis = millis; |
||||
} |
||||
|
||||
@DateTimeFormat(style="S-") |
||||
public Long getMillisAnnotated() { |
||||
return millisAnnotated; |
||||
} |
||||
|
||||
public void setMillisAnnotated(@DateTimeFormat(style="S-") Long millisAnnotated) { |
||||
this.millisAnnotated = millisAnnotated; |
||||
} |
||||
|
||||
public Date getIsoDate() { |
||||
return isoDate; |
||||
} |
||||
|
||||
public void setIsoDate(Date isoDate) { |
||||
this.isoDate = isoDate; |
||||
} |
||||
|
||||
public Date getIsoTime() { |
||||
return isoTime; |
||||
} |
||||
|
||||
public void setIsoTime(Date isoTime) { |
||||
this.isoTime = isoTime; |
||||
} |
||||
|
||||
public Date getIsoDateTime() { |
||||
return isoDateTime; |
||||
} |
||||
|
||||
public void setIsoDateTime(Date isoDateTime) { |
||||
this.isoDateTime = isoDateTime; |
||||
} |
||||
|
||||
public List<SimpleDateBean> getChildren() { |
||||
return children; |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue