date time format code review
This commit is contained in:
@@ -22,18 +22,22 @@ import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Declares that a field should be formatted as a date time.
|
||||
* Supports formatting by style pattern or by format pattern string.
|
||||
* Supports formatting by style pattern, custom format pattern string, or ISO date time pattern.
|
||||
* 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.
|
||||
* For style-based formatting, set the {@link #style()} attribute to be the style pattern code.
|
||||
* The first character of the code 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 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.
|
||||
* For pattern-based formatting, set the {@link #pattern()} attribute to be the DateTime pattern, such as <code>yyyy/mm/dd h:mm:ss a</code>.
|
||||
* <p>
|
||||
* If no annotation attributes are specified, the default format applied is style-based with a style code of 'SS' (short date, short time).
|
||||
* For ISO-based formatting, set the {@link #iso()} attribute to be the desired {@link ISO} format, such as {@link ISO#DATE}.
|
||||
* <p>
|
||||
* Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient one for your formatting needs).
|
||||
* When the pattern attribute is specified, it takes precedence over both the style and ISO attribute.
|
||||
* When the iso attribute is specified, if takes precedence over the style attribute.
|
||||
* When 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
|
||||
@@ -50,9 +54,44 @@ public @interface DateTimeFormat {
|
||||
String style() default "SS";
|
||||
|
||||
/**
|
||||
* A pattern String to apply to format the field.
|
||||
* Set this attribute or the style attribute, not both.
|
||||
*/
|
||||
String pattern() default "";
|
||||
|
||||
/**
|
||||
* A ISO pattern to apply to format the field.
|
||||
* The possible ISO patterns are defined in the {@link ISO} enum.
|
||||
* Defaults to ISO.NONE, indicating this attribute should be ignored.
|
||||
*/
|
||||
ISO iso() default ISO.NONE;
|
||||
|
||||
/**
|
||||
* Common ISO date time format patterns.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
public enum ISO {
|
||||
|
||||
/**
|
||||
* The most common ISO Date Format <code>yyyy-MM-dd</code> e.g. 2000-10-31.
|
||||
*/
|
||||
DATE,
|
||||
|
||||
/**
|
||||
* The most common ISO Time Format <code>hh:mm:ss.SSSZ</code> e.g. 01:30:00.000-05:00.
|
||||
*/
|
||||
TIME,
|
||||
|
||||
/**
|
||||
* The most common ISO DateTime Format <code>yyyy-MM-dd'T'hh:mm:ss.SSSZ</code> e.g. 2000-10-31 01:30:00.000-05:00.
|
||||
* The default if no annotation value is specified.
|
||||
*/
|
||||
DATE_TIME,
|
||||
|
||||
/**
|
||||
* Indicates that no ISO-based format pattern should be applied.
|
||||
*/
|
||||
NONE
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Declares that a field should be formatted as a ISO date time.
|
||||
* Can be applied to <code>java.util.Date</code>, <code>java.util.Calendar</code>, <code>java.long.Long</code>, or Joda Time fields.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
@Target( { ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface ISODateTimeFormat {
|
||||
|
||||
/**
|
||||
* The ISO style to use to format the date time.
|
||||
* Defaults to {@link ISO#DATE_TIME}.
|
||||
*/
|
||||
ISO value() default ISO.DATE_TIME;
|
||||
|
||||
public enum ISO {
|
||||
|
||||
/**
|
||||
* The most common ISO Date Format <code>yyyy-MM-dd</code> e.g. 2000-10-31.
|
||||
*/
|
||||
DATE,
|
||||
|
||||
/**
|
||||
* The most common ISO Time Format <code>hh:mm:ss.SSSZ</code> e.g. 01:30:00.000-05:00.
|
||||
*/
|
||||
TIME,
|
||||
|
||||
/**
|
||||
* The most common ISO DateTime Format <code>yyyy-MM-dd'T'hh:mm:ss.SSSZ</code> e.g. 2000-10-31 01:30:00.000-05:00.
|
||||
* The default if no annotation value is specified.
|
||||
*/
|
||||
DATE_TIME
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.joda;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
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;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.joda.time.ReadableInstant;
|
||||
import org.joda.time.ReadablePartial;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.springframework.format.AnnotationFormatterFactory;
|
||||
import org.springframework.format.Parser;
|
||||
import org.springframework.format.Printer;
|
||||
|
||||
/**
|
||||
* Base class for annotation-based Joda DateTime formatters.
|
||||
* Can format any Joda {@link ReadableInstant} or {@link ReadablePartial} property, as well as standard {@link Date}, {@link Calendar}, and Long properties.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @param <A> the format annotation parameter type to be declared by subclasses
|
||||
*/
|
||||
abstract class AbstractDateTimeAnnotationFormatterFactory<A extends Annotation> implements AnnotationFormatterFactory<A> {
|
||||
|
||||
private final Set<Class<?>> fieldTypes;
|
||||
|
||||
public AbstractDateTimeAnnotationFormatterFactory() {
|
||||
this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
|
||||
}
|
||||
|
||||
public Set<Class<?>> getFieldTypes() {
|
||||
return this.fieldTypes;
|
||||
}
|
||||
|
||||
public Printer<?> getPrinter(A annotation, Class<?> fieldType) {
|
||||
DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation);
|
||||
if (ReadableInstant.class.isAssignableFrom(fieldType)) {
|
||||
return new ReadableInstantPrinter(formatter);
|
||||
} else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
|
||||
return new ReadablePartialPrinter(formatter);
|
||||
} else if (Calendar.class.isAssignableFrom(fieldType)) {
|
||||
// assumes Calendar->ReadableInstant converter is registered
|
||||
return new ReadableInstantPrinter(formatter);
|
||||
} else {
|
||||
// assumes Date->Long converter is registered
|
||||
return new MillisecondInstantPrinter(formatter);
|
||||
}
|
||||
}
|
||||
|
||||
public Parser<DateTime> getParser(A annotation, Class<?> propertyType) {
|
||||
return new DateTimeParser(configureDateTimeFormatterFrom(annotation));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook method subclasses should override to configure the Joda {@link DateTimeFormatter} from the <code>annotation</code> values.
|
||||
* @param annotation the annotation containing formatter configuration rules
|
||||
* @return the configured date time formatter
|
||||
*/
|
||||
protected abstract DateTimeFormatter configureDateTimeFormatterFrom(A annotation);
|
||||
|
||||
// internal helpers
|
||||
|
||||
private Set<Class<?>> createFieldTypes() {
|
||||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(8);
|
||||
fieldTypes.add(LocalDate.class);
|
||||
fieldTypes.add(LocalTime.class);
|
||||
fieldTypes.add(LocalDateTime.class);
|
||||
fieldTypes.add(DateTime.class);
|
||||
fieldTypes.add(DateMidnight.class);
|
||||
fieldTypes.add(Date.class);
|
||||
fieldTypes.add(Calendar.class);
|
||||
fieldTypes.add(Long.class);
|
||||
return fieldTypes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,8 +15,25 @@
|
||||
*/
|
||||
package org.springframework.format.datetime.joda;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
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;
|
||||
import org.joda.time.LocalTime;
|
||||
import org.joda.time.ReadableInstant;
|
||||
import org.joda.time.ReadablePartial;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.springframework.format.AnnotationFormatterFactory;
|
||||
import org.springframework.format.Parser;
|
||||
import org.springframework.format.Printer;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
|
||||
/**
|
||||
* Formats fields annotated with the {@link DateTimeFormat} annotation.
|
||||
@@ -24,12 +41,57 @@ import org.springframework.format.annotation.DateTimeFormat;
|
||||
* @since 3.0
|
||||
* @see DateTimeFormat
|
||||
*/
|
||||
public final class DateTimeFormatAnnotationFormatterFactory extends AbstractDateTimeAnnotationFormatterFactory<DateTimeFormat> {
|
||||
public final class DateTimeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||
|
||||
protected DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) {
|
||||
String pattern = annotation.pattern();
|
||||
if (!pattern.isEmpty()) {
|
||||
return forPattern(pattern);
|
||||
private final Set<Class<?>> fieldTypes;
|
||||
|
||||
public DateTimeFormatAnnotationFormatterFactory() {
|
||||
this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
|
||||
}
|
||||
|
||||
public Set<Class<?>> getFieldTypes() {
|
||||
return this.fieldTypes;
|
||||
}
|
||||
|
||||
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
DateTimeFormatter formatter = configureDateTimeFormatterFrom(annotation);
|
||||
if (ReadableInstant.class.isAssignableFrom(fieldType)) {
|
||||
return new ReadableInstantPrinter(formatter);
|
||||
} else if (ReadablePartial.class.isAssignableFrom(fieldType)) {
|
||||
return new ReadablePartialPrinter(formatter);
|
||||
} else if (Calendar.class.isAssignableFrom(fieldType)) {
|
||||
// assumes Calendar->ReadableInstant converter is registered
|
||||
return new ReadableInstantPrinter(formatter);
|
||||
} else {
|
||||
// assumes Date->Long converter is registered
|
||||
return new MillisecondInstantPrinter(formatter);
|
||||
}
|
||||
}
|
||||
|
||||
public Parser<DateTime> getParser(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
return new DateTimeParser(configureDateTimeFormatterFrom(annotation));
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
private Set<Class<?>> createFieldTypes() {
|
||||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(8);
|
||||
fieldTypes.add(LocalDate.class);
|
||||
fieldTypes.add(LocalTime.class);
|
||||
fieldTypes.add(LocalDateTime.class);
|
||||
fieldTypes.add(DateTime.class);
|
||||
fieldTypes.add(DateMidnight.class);
|
||||
fieldTypes.add(Date.class);
|
||||
fieldTypes.add(Calendar.class);
|
||||
fieldTypes.add(Long.class);
|
||||
return fieldTypes;
|
||||
}
|
||||
|
||||
private DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) {
|
||||
if (!annotation.pattern().isEmpty()) {
|
||||
return forPattern(annotation.pattern());
|
||||
} else if (annotation.iso() != ISO.NONE) {
|
||||
return forISO(annotation.iso());
|
||||
} else {
|
||||
return forStyle(annotation.style());
|
||||
}
|
||||
@@ -38,6 +100,16 @@ public final class DateTimeFormatAnnotationFormatterFactory extends AbstractDate
|
||||
private DateTimeFormatter forPattern(String pattern) {
|
||||
return org.joda.time.format.DateTimeFormat.forPattern(pattern);
|
||||
}
|
||||
|
||||
private DateTimeFormatter forISO(ISO iso) {
|
||||
if (iso == ISO.DATE) {
|
||||
return org.joda.time.format.ISODateTimeFormat.date();
|
||||
} else if (iso == ISO.TIME) {
|
||||
return org.joda.time.format.ISODateTimeFormat.time();
|
||||
} else {
|
||||
return org.joda.time.format.ISODateTimeFormat.dateTime();
|
||||
}
|
||||
}
|
||||
|
||||
private DateTimeFormatter forStyle(String style) {
|
||||
return org.joda.time.format.DateTimeFormat.forStyle(style);
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 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.joda;
|
||||
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.springframework.format.annotation.ISODateTimeFormat;
|
||||
import org.springframework.format.annotation.ISODateTimeFormat.ISO;
|
||||
|
||||
/**
|
||||
* Formats fields annotated with the {@link ISODateTimeFormat} annotation.
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
* @see ISODateTimeFormat
|
||||
*/
|
||||
public final class ISODateTimeFormatAnnotationFormatterFactory extends AbstractDateTimeAnnotationFormatterFactory<ISODateTimeFormat> {
|
||||
|
||||
protected DateTimeFormatter configureDateTimeFormatterFrom(ISODateTimeFormat annotation) {
|
||||
ISO format = annotation.value();
|
||||
if (format == ISO.DATE) {
|
||||
return org.joda.time.format.ISODateTimeFormat.date();
|
||||
} else if (format == ISO.TIME) {
|
||||
return org.joda.time.format.ISODateTimeFormat.time();
|
||||
} else {
|
||||
return org.joda.time.format.ISODateTimeFormat.dateTime();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -109,7 +109,6 @@ public class JodaTimeFormattingConfigurer {
|
||||
formatterRegistry.addFormatterForFieldType(Date.class, new MillisecondInstantPrinter(jodaDateTimeFormatter), dateTimeParser);
|
||||
|
||||
formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
|
||||
formatterRegistry.addFormatterForFieldAnnotation(new ISODateTimeFormatAnnotationFormatterFactory());
|
||||
}
|
||||
|
||||
// internal helpers
|
||||
|
||||
Reference in New Issue
Block a user