|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2012 the original author or authors. |
|
|
|
* Copyright 2002-2013 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -13,6 +13,7 @@ |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
* limitations under the License. |
|
|
|
* limitations under the License. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.format.datetime.joda; |
|
|
|
package org.springframework.format.datetime.joda; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Calendar; |
|
|
|
import java.util.Calendar; |
|
|
|
@ -35,7 +36,7 @@ import org.springframework.format.Printer; |
|
|
|
import org.springframework.format.annotation.DateTimeFormat.ISO; |
|
|
|
import org.springframework.format.annotation.DateTimeFormat.ISO; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Configures Joda Time's formatting system for use with Spring. |
|
|
|
* Configures Joda-Time's formatting system for use with Spring. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Keith Donald |
|
|
|
* @author Keith Donald |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
@ -51,15 +52,18 @@ import org.springframework.format.annotation.DateTimeFormat.ISO; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static enum Type {DATE, TIME, DATE_TIME} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* User defined formatters. |
|
|
|
* User defined formatters. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private Map<Type, DateTimeFormatter> formatters = new HashMap<Type, DateTimeFormatter>(); |
|
|
|
private final Map<Type, DateTimeFormatter> formatters = new HashMap<Type, DateTimeFormatter>(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Factories used when specific formatters have not been specified. |
|
|
|
* Factories used when specific formatters have not been specified. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private Map<Type, DateTimeFormatterFactory> factories; |
|
|
|
private final Map<Type, DateTimeFormatterFactory> factories; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public JodaTimeFormatterRegistrar() { |
|
|
|
public JodaTimeFormatterRegistrar() { |
|
|
|
@ -70,12 +74,24 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set whether standard ISO formatting should be applied to all date/time types. |
|
|
|
|
|
|
|
* Default is "false" (no). |
|
|
|
|
|
|
|
* <p>If set to "true", the "dateStyle", "timeStyle" and "dateTimeStyle" |
|
|
|
|
|
|
|
* properties are effectively ignored. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setUseIsoFormat(boolean useIsoFormat) { |
|
|
|
|
|
|
|
this.factories.get(Type.DATE).setIso(useIsoFormat ? ISO.DATE : null); |
|
|
|
|
|
|
|
this.factories.get(Type.TIME).setIso(useIsoFormat ? ISO.TIME : null); |
|
|
|
|
|
|
|
this.factories.get(Type.DATE_TIME).setIso(useIsoFormat ? ISO.DATE_TIME : null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the default format style of Joda {@link LocalDate} objects. |
|
|
|
* Set the default format style of Joda {@link LocalDate} objects. |
|
|
|
* Default is {@link DateTimeFormat#shortDate()}. |
|
|
|
* Default is {@link DateTimeFormat#shortDate()}. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setDateStyle(String dateStyle) { |
|
|
|
public void setDateStyle(String dateStyle) { |
|
|
|
this.factories.get(Type.DATE).setStyle(dateStyle+"-"); |
|
|
|
this.factories.get(Type.DATE).setStyle(dateStyle + "-"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -83,7 +99,7 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
* Default is {@link DateTimeFormat#shortTime()}. |
|
|
|
* Default is {@link DateTimeFormat#shortTime()}. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setTimeStyle(String timeStyle) { |
|
|
|
public void setTimeStyle(String timeStyle) { |
|
|
|
this.factories.get(Type.TIME).setStyle("-"+timeStyle); |
|
|
|
this.factories.get(Type.TIME).setStyle("-" + timeStyle); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -95,25 +111,14 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
this.factories.get(Type.DATE_TIME).setStyle(dateTimeStyle); |
|
|
|
this.factories.get(Type.DATE_TIME).setStyle(dateTimeStyle); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set whether standard ISO formatting should be applied to all Date/Time types. |
|
|
|
|
|
|
|
* Default is false (no). |
|
|
|
|
|
|
|
* If set to true, the dateStyle, timeStyle, and dateTimeStyle properties are ignored. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void setUseIsoFormat(boolean useIsoFormat) { |
|
|
|
|
|
|
|
this.factories.get(Type.DATE).setIso(useIsoFormat ? ISO.DATE : null); |
|
|
|
|
|
|
|
this.factories.get(Type.TIME).setIso(useIsoFormat ? ISO.TIME : null); |
|
|
|
|
|
|
|
this.factories.get(Type.DATE_TIME).setIso(useIsoFormat ? ISO.DATE_TIME : null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Set the formatter that will be used for objects representing date values. |
|
|
|
* Set the formatter that will be used for objects representing date values. |
|
|
|
* <p>This formatter will be used for the {@link LocalDate} type. When specified |
|
|
|
* <p>This formatter will be used for the {@link LocalDate} type. When specified |
|
|
|
* the {@link #setDateStyle(String) dateStyle} and |
|
|
|
* the {@link #setDateStyle(String) dateStyle} and |
|
|
|
* {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. |
|
|
|
* {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. |
|
|
|
* @param formatter the formatter to use |
|
|
|
* @param formatter the formatter to use |
|
|
|
* @see #setTimeFormatter(DateTimeFormatter) |
|
|
|
* @see #setTimeFormatter |
|
|
|
* @see #setDateTimeFormatter(DateTimeFormatter) |
|
|
|
* @see #setDateTimeFormatter |
|
|
|
* @since 3.2 |
|
|
|
* @since 3.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setDateFormatter(DateTimeFormatter formatter) { |
|
|
|
public void setDateFormatter(DateTimeFormatter formatter) { |
|
|
|
@ -126,8 +131,8 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
* the {@link #setTimeStyle(String) timeStyle} and |
|
|
|
* the {@link #setTimeStyle(String) timeStyle} and |
|
|
|
* {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. |
|
|
|
* {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. |
|
|
|
* @param formatter the formatter to use |
|
|
|
* @param formatter the formatter to use |
|
|
|
* @see #setDateFormatter(DateTimeFormatter) |
|
|
|
* @see #setDateFormatter |
|
|
|
* @see #setDateTimeFormatter(DateTimeFormatter) |
|
|
|
* @see #setDateTimeFormatter |
|
|
|
* @since 3.2 |
|
|
|
* @since 3.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setTimeFormatter(DateTimeFormatter formatter) { |
|
|
|
public void setTimeFormatter(DateTimeFormatter formatter) { |
|
|
|
@ -141,14 +146,15 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
* the {@link #setDateTimeStyle(String) dateTimeStyle} and |
|
|
|
* the {@link #setDateTimeStyle(String) dateTimeStyle} and |
|
|
|
* {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. |
|
|
|
* {@link #setUseIsoFormat(boolean) useIsoFormat} properties will be ignored. |
|
|
|
* @param formatter the formatter to use |
|
|
|
* @param formatter the formatter to use |
|
|
|
* @see #setDateFormatter(DateTimeFormatter) |
|
|
|
* @see #setDateFormatter |
|
|
|
* @see #setTimeFormatter(DateTimeFormatter) |
|
|
|
* @see #setTimeFormatter |
|
|
|
* @since 3.2 |
|
|
|
* @since 3.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setDateTimeFormatter(DateTimeFormatter formatter) { |
|
|
|
public void setDateTimeFormatter(DateTimeFormatter formatter) { |
|
|
|
this.formatters.put(Type.DATE_TIME, formatter); |
|
|
|
this.formatters.put(Type.DATE_TIME, formatter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void registerFormatters(FormatterRegistry registry) { |
|
|
|
public void registerFormatters(FormatterRegistry registry) { |
|
|
|
JodaTimeConverters.registerConverters(registry); |
|
|
|
JodaTimeConverters.registerConverters(registry); |
|
|
|
|
|
|
|
|
|
|
|
@ -176,26 +182,25 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
new DateTimeParser(dateTimeFormatter), |
|
|
|
new DateTimeParser(dateTimeFormatter), |
|
|
|
ReadableInstant.class); |
|
|
|
ReadableInstant.class); |
|
|
|
|
|
|
|
|
|
|
|
// In order to retain back compatibility we only register Date/Calendar
|
|
|
|
// In order to retain backwards compatibility we only register Date/Calendar
|
|
|
|
// types when a user defined formatter is specified (see SPR-10105)
|
|
|
|
// types when a user defined formatter is specified (see SPR-10105)
|
|
|
|
if(this.formatters.containsKey(Type.DATE_TIME)) { |
|
|
|
if( this.formatters.containsKey(Type.DATE_TIME)) { |
|
|
|
addFormatterForFields(registry, |
|
|
|
addFormatterForFields(registry, |
|
|
|
new ReadableInstantPrinter(dateTimeFormatter), |
|
|
|
new ReadableInstantPrinter(dateTimeFormatter), |
|
|
|
new DateTimeParser(dateTimeFormatter), |
|
|
|
new DateTimeParser(dateTimeFormatter), |
|
|
|
Date.class, Calendar.class); |
|
|
|
Date.class, Calendar.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
registry.addFormatterForFieldAnnotation( |
|
|
|
registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); |
|
|
|
new JodaDateTimeFormatAnnotationFormatterFactory()); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private DateTimeFormatter getFormatter(Type type) { |
|
|
|
private DateTimeFormatter getFormatter(Type type) { |
|
|
|
DateTimeFormatter formatter = this.formatters.get(type); |
|
|
|
DateTimeFormatter formatter = this.formatters.get(type); |
|
|
|
if(formatter != null) { |
|
|
|
if (formatter != null) { |
|
|
|
return formatter; |
|
|
|
return formatter; |
|
|
|
} |
|
|
|
} |
|
|
|
DateTimeFormatter fallbackFormatter = getFallbackFormatter(type); |
|
|
|
DateTimeFormatter fallbackFormatter = getFallbackFormatter(type); |
|
|
|
return this.factories.get(type).createDateTimeFormatter(fallbackFormatter ); |
|
|
|
return this.factories.get(type).createDateTimeFormatter(fallbackFormatter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private DateTimeFormatter getFallbackFormatter(Type type) { |
|
|
|
private DateTimeFormatter getFallbackFormatter(Type type) { |
|
|
|
@ -208,10 +213,10 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar { |
|
|
|
|
|
|
|
|
|
|
|
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer, |
|
|
|
private void addFormatterForFields(FormatterRegistry registry, Printer<?> printer, |
|
|
|
Parser<?> parser, Class<?>... fieldTypes) { |
|
|
|
Parser<?> parser, Class<?>... fieldTypes) { |
|
|
|
|
|
|
|
|
|
|
|
for (Class<?> fieldType : fieldTypes) { |
|
|
|
for (Class<?> fieldType : fieldTypes) { |
|
|
|
registry.addFormatterForFieldType(fieldType, printer, parser); |
|
|
|
registry.addFormatterForFieldType(fieldType, printer, parser); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static enum Type {DATE, TIME, DATE_TIME} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|