updated favoring style pattern string instead of Style enum for DateTimeFormat

This commit is contained in:
Keith Donald 2009-11-09 21:07:41 +00:00
parent 7ed6c164b6
commit 6f4112af80
7 changed files with 80 additions and 116 deletions

View File

@ -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 <code>java.util.Date</code>, <code>java.util.Calendar</code>, <code>java.long.Long</code>, or Joda Time fields.
* <p>
* For style-based formatting, set the <code>style</code> 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 '-'.
* <p>
* For style-based formatting:
* <ul>
* <li>Set <code>dateStyle</code> attribute to specify the style of the <i>date</i> portion of the DateTime.
* <li>Set <code>timeStyle</code> attribute to specify the style of the <i>time</i> portion of the DateTime.
* <li>The default for both dateStyle and timeStyle if not specified is {@link Style#NONE}.
* </ul>
* For pattern-based formatting, set the <code>pattern</code> attribute to be the DateTime pattern, such as <code>yyyy/mm/dd h:mm:ss a</code>.
* If the pattern attribute is specified, it takes precedence over the style attribute.
* <p>
* 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 <code>*Style</code> 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.
* <br>Example short dateStyle: Locale.US="M/d/yy" e.g. 10/31/2009
* <br>Example short timeStyle: Locale.US="h:mm a" e.g. 1:30 PM
*/
SHORT {
public String toString() {
return "S";
}
},
/**
* The medium format style.
* <br>Example medium dateStyle: Locale.US="MMM d, yyyy" e.g Oct 31, 2009
* <br>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.
* <br>Example long dateStyle: Locale.US="MMMM d, yyyy" e.g October 31, 2009
* <br>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.
* <br>Example full dateStyle: Locale.US="EEEE, MMMM d, yyyy" e.g. Saturday, October 31, 2009
* <br>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 "-";
}
}
}
}

View File

@ -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 <code>yyyy-MM-dd</code> e.g. 2000-10-31.

View File

@ -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);
}
}

View File

@ -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<ISODateTimeFormat> {
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();

View File

@ -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;
}
}
}

View File

@ -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;
}

View File

@ -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);
}
}