From 6f4112af80540690f651f6e8b5044f48b1f9c17a Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Mon, 9 Nov 2009 21:07:41 +0000 Subject: [PATCH] updated favoring style pattern string instead of Style enum for DateTimeFormat --- .../format/annotation/DateTimeFormat.java | 97 +++---------------- .../format/annotation/ISODateTimeFormat.java | 6 +- ...eTimeFormatAnnotationFormatterFactory.java | 17 +--- ...eTimeFormatAnnotationFormatterFactory.java | 8 +- .../joda/JodaTimeFormattingTests.java | 62 +++++++++--- .../FormattingConversionServiceTests.java | 4 +- .../web/servlet/config/MvcNamespaceTests.java | 2 +- 7 files changed, 80 insertions(+), 116 deletions(-) diff --git a/org.springframework.context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java b/org.springframework.context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java index fcf430d5811..01280a07b5a 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java +++ b/org.springframework.context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java @@ -22,104 +22,37 @@ import java.lang.annotation.Target; /** * Declares that a field should be formatted as a date time. - * Supports formatting by {@link Style} or by pattern string. + * Supports formatting by style pattern or by format pattern string. + * Can be applied to java.util.Date, java.util.Calendar, java.long.Long, or Joda Time fields. + *

+ * For style-based formatting, set the style attribute to be the style pattern. + * The first character is the date style, and the second character is the time style. + * Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full. + * A date or time may be omitted by specifying the style character '-'. *

- * For style-based formatting: - *

* For pattern-based formatting, set the pattern attribute to be the DateTime pattern, such as yyyy/mm/dd h:mm:ss a. + * If the pattern attribute is specified, it takes precedence over the style attribute. *

- * If no annotation attributes are specified, the default format applied is style-based with dateStyle={@link Style#SHORT} and timeStyle={@link Style#SHORT}. + * If no annotation attributes are specified, the default format applied is style-based with a style code of 'SS' (short date, short time). * * @author Keith Donald * @since 3.0 + * @see org.joda.time.format.DateTimeFormat */ @Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER }) @Retention(RetentionPolicy.RUNTIME) public @interface DateTimeFormat { /** - * The style to use for formatting the date portion of the property. - * Defaults to {@link Style#NONE}. + * The style pattern to use to format the field. + * Defaults to 'SS' for short date time. */ - Style dateStyle() default Style.NONE; + String style() default "SS"; /** - * The style to use for formatting the time portion of the property. - * Defaults to {@link Style#NONE}. - */ - Style timeStyle() default Style.NONE; - - /** - * A pattern String that defines a custom format for the property. - * Use this method or the *Style methods, not both. + * A pattern String to apply to format the field. + * Set this attribute or the style attribute, not both. */ String pattern() default ""; - - /** - * Supported DateTimeFormat styles. - */ - public enum Style { - /** - * The short format style. - *
Example short dateStyle: Locale.US="M/d/yy" e.g. 10/31/2009 - *
Example short timeStyle: Locale.US="h:mm a" e.g. 1:30 PM - */ - SHORT { - public String toString() { - return "S"; - } - }, - - /** - * The medium format style. - *
Example medium dateStyle: Locale.US="MMM d, yyyy" e.g Oct 31, 2009 - *
Example medium timeStyle: Locale.US="h:mm:ss a" e.g. 1:30:00 PM - */ - MEDIUM { - public String toString() { - return "M"; - } - }, - - /** - * The long format style. - *
Example long dateStyle: Locale.US="MMMM d, yyyy" e.g October 31, 2009 - *
Example long timeStyle: Locale.US="h:mm:ss a z" e.g. 1:30:00 PM Eastern Standard Time - */ - LONG { - public String toString() { - return "L"; - } - }, - - /** - * The full format style. - *
Example full dateStyle: Locale.US="EEEE, MMMM d, yyyy" e.g. Saturday, October 31, 2009 - *
Example full timeStyle: Locale.US="h:mm:ss a z" e.g 1:30:00 PM Eastern Standard Time - */ - FULL { - public String toString() { - return "F"; - } - }, - - /** - * The none format style. - * A dateStyle specified with this value results in date fields such as mm, dd, and yyyy being ignored. - * A timeStyle specified with this value results in time fields such as hh, mm being ignored. - * If both dateStyle and timeStyle are set to this value and the pattern attribute is also not specified, - * a default value for each is selected based on the type of field being annotated. - */ - NONE { - public String toString() { - return "-"; - } - } - } - } diff --git a/org.springframework.context/src/main/java/org/springframework/format/annotation/ISODateTimeFormat.java b/org.springframework.context/src/main/java/org/springframework/format/annotation/ISODateTimeFormat.java index 71a32ae834e..938a971d60e 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/annotation/ISODateTimeFormat.java +++ b/org.springframework.context/src/main/java/org/springframework/format/annotation/ISODateTimeFormat.java @@ -31,11 +31,11 @@ public @interface ISODateTimeFormat { /** * The ISO style to use to format the date time. - * Defaults to {@link Style#DATE_TIME}. + * Defaults to {@link ISO#DATE_TIME}. */ - Style value() default Style.DATE_TIME; + ISO value() default ISO.DATE_TIME; - public enum Style { + public enum ISO { /** * The most common ISO Date Format yyyy-MM-dd e.g. 2000-10-31. diff --git a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java index 58f2af0ba21..3e6b42b7384 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java +++ b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java @@ -17,7 +17,6 @@ package org.springframework.format.datetime.joda; import org.joda.time.format.DateTimeFormatter; import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.format.annotation.DateTimeFormat.Style; /** * Formats fields annotated with the {@link DateTimeFormat} annotation. @@ -32,22 +31,16 @@ public final class DateTimeFormatAnnotationFormatterFactory extends AbstractDate if (!pattern.isEmpty()) { return forPattern(pattern); } else { - Style dateStyle = annotation.dateStyle(); - Style timeStyle = annotation.timeStyle(); - if (Style.NONE == dateStyle && Style.NONE == timeStyle) { - return forDateTimeStyle(Style.SHORT, Style.SHORT); - } else { - return forDateTimeStyle(dateStyle, timeStyle); - } + return forStyle(annotation.style()); } } - private DateTimeFormatter forDateTimeStyle(Style dateStyle, Style timeStyle) { - return org.joda.time.format.DateTimeFormat.forStyle(dateStyle.toString() + timeStyle.toString()); - } - private DateTimeFormatter forPattern(String pattern) { return org.joda.time.format.DateTimeFormat.forPattern(pattern); } + private DateTimeFormatter forStyle(String style) { + return org.joda.time.format.DateTimeFormat.forStyle(style); + } + } \ No newline at end of file diff --git a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/ISODateTimeFormatAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/ISODateTimeFormatAnnotationFormatterFactory.java index 8112160d5f0..46958ab0d09 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/ISODateTimeFormatAnnotationFormatterFactory.java +++ b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/ISODateTimeFormatAnnotationFormatterFactory.java @@ -17,7 +17,7 @@ package org.springframework.format.datetime.joda; import org.joda.time.format.DateTimeFormatter; import org.springframework.format.annotation.ISODateTimeFormat; -import org.springframework.format.annotation.ISODateTimeFormat.Style; +import org.springframework.format.annotation.ISODateTimeFormat.ISO; /** * Formats fields annotated with the {@link ISODateTimeFormat} annotation. @@ -28,10 +28,10 @@ import org.springframework.format.annotation.ISODateTimeFormat.Style; public final class ISODateTimeFormatAnnotationFormatterFactory extends AbstractDateTimeAnnotationFormatterFactory { protected DateTimeFormatter configureDateTimeFormatterFrom(ISODateTimeFormat annotation) { - Style style = annotation.value(); - if (style == Style.DATE) { + ISO style = annotation.value(); + if (style == ISO.DATE) { return org.joda.time.format.ISODateTimeFormat.date(); - } else if (style == Style.TIME) { + } else if (style == ISO.TIME) { return org.joda.time.format.ISODateTimeFormat.time(); } else { return org.joda.time.format.ISODateTimeFormat.dateTime(); diff --git a/org.springframework.context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java b/org.springframework.context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java index 25368cb7c9e..634995904b0 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/datetime/joda/JodaTimeFormattingTests.java @@ -18,7 +18,7 @@ import org.springframework.beans.MutablePropertyValues; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.ISODateTimeFormat; -import org.springframework.format.annotation.DateTimeFormat.Style; +import org.springframework.format.annotation.ISODateTimeFormat.ISO; import org.springframework.format.support.FormattingConversionService; import org.springframework.validation.DataBinder; @@ -137,6 +137,15 @@ public class JodaTimeFormattingTests { assertEquals("Oct 31, 2009 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotated")); } + @Test + public void testBindDateTimeAnnotatedDefault() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.addPropertyValue("dateTimeAnnotatedDefault", "10/31/09 12:00 PM"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertEquals("10/31/09 12:00 PM", binder.getBindingResult().getFieldValue("dateTimeAnnotatedDefault")); + } + @Test public void testBindDate() { MutablePropertyValues propertyValues = new MutablePropertyValues(); @@ -218,52 +227,67 @@ public class JodaTimeFormattingTests { assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTime")); } + @Test + public void testBindISODateTimeDefault() { + MutablePropertyValues propertyValues = new MutablePropertyValues(); + propertyValues.addPropertyValue("isoDateTimeDefault", "2009-10-31T12:00:00.000Z"); + binder.bind(propertyValues); + assertEquals(0, binder.getBindingResult().getErrorCount()); + assertEquals("2009-10-31T07:00:00.000-05:00", binder.getBindingResult().getFieldValue("isoDateTimeDefault")); + } + public static class JodaTimeBean { private LocalDate localDate; - @DateTimeFormat(dateStyle = Style.MEDIUM) + @DateTimeFormat(style="M-") private LocalDate localDateAnnotated; private LocalTime localTime; - @DateTimeFormat(timeStyle = Style.MEDIUM) + @DateTimeFormat(style="-M") private LocalTime localTimeAnnotated; private LocalDateTime localDateTime; - @DateTimeFormat(dateStyle = Style.FULL, timeStyle = Style.FULL) + @DateTimeFormat(style="FF") private LocalDateTime localDateTimeAnnotated; private DateTime dateTime; - @DateTimeFormat(dateStyle = Style.MEDIUM, timeStyle = Style.SHORT) + @DateTimeFormat(style="MS") private DateTime dateTimeAnnotated; + @DateTimeFormat + private DateTime dateTimeAnnotatedDefault; + private Date date; - @DateTimeFormat(dateStyle = Style.SHORT) + @DateTimeFormat(style="S-") private Date dateAnnotated; private Calendar calendar; - @DateTimeFormat(dateStyle = Style.SHORT) + @DateTimeFormat(style="S-") private Calendar calendarAnnotated; private Long millis; - @DateTimeFormat(dateStyle = Style.SHORT) + @DateTimeFormat(style="S-") private Long millisAnnotated; - @ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE) + @ISODateTimeFormat(ISO.DATE) private LocalDate isoDate; - @ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.TIME) + @ISODateTimeFormat(ISO.TIME) private LocalTime isoTime; - @ISODateTimeFormat(org.springframework.format.annotation.ISODateTimeFormat.Style.DATE_TIME) + @ISODateTimeFormat(ISO.DATE_TIME) private DateTime isoDateTime; + @ISODateTimeFormat + private DateTime isoDateTimeDefault; + public LocalDate getLocalDate() { return localDate; } @@ -328,6 +352,14 @@ public class JodaTimeFormattingTests { this.dateTimeAnnotated = dateTimeAnnotated; } + public DateTime getDateTimeAnnotatedDefault() { + return dateTimeAnnotatedDefault; + } + + public void setDateTimeAnnotatedDefault(DateTime dateTimeAnnotatedDefault) { + this.dateTimeAnnotatedDefault = dateTimeAnnotatedDefault; + } + public Date getDate() { return date; } @@ -400,5 +432,13 @@ public class JodaTimeFormattingTests { this.isoDateTime = isoDateTime; } + public DateTime getIsoDateTimeDefault() { + return isoDateTimeDefault; + } + + public void setIsoDateTimeDefault(DateTime isoDateTimeDefault) { + this.isoDateTimeDefault = isoDateTimeDefault; + } + } } \ No newline at end of file diff --git a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java index a2d2f974e6d..f45bacc4d53 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java @@ -31,12 +31,10 @@ import org.junit.Test; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; -import org.springframework.format.annotation.DateTimeFormat.Style; import org.springframework.format.datetime.joda.DateTimeFormatAnnotationFormatterFactory; import org.springframework.format.datetime.joda.DateTimeParser; import org.springframework.format.datetime.joda.ReadablePartialPrinter; import org.springframework.format.number.IntegerFormatter; -import org.springframework.format.support.FormattingConversionService; /** * @author Keith Donald @@ -106,7 +104,7 @@ public class FormattingConversionServiceTests { private static class Model { @SuppressWarnings("unused") - @org.springframework.format.annotation.DateTimeFormat(dateStyle = Style.SHORT) + @org.springframework.format.annotation.DateTimeFormat(style="S-") public Date date; } diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java index 1fd6a869e5f..b05e3e03406 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java @@ -59,7 +59,7 @@ public class MvcNamespaceTests { public static class TestController { @RequestMapping - public void testBind(@RequestParam @DateTimeFormat(dateStyle=Style.MEDIUM) Date date) { + public void testBind(@RequestParam @DateTimeFormat(dateStyle=ISO.MEDIUM) Date date) { System.out.println(date); } }