From b5d44e1d15dc657ba51697c8de46ec1cc46fc71b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 23 Apr 2013 13:50:25 +0200 Subject: [PATCH] Revised Joda-Time support (as a side effect of JSR-310 support in Spring 4.0) Issue: SPR-9641 --- .../format/annotation/DateTimeFormat.java | 52 ++++--- .../format/datetime/DateFormatter.java | 24 ++-- .../datetime/DateFormatterRegistrar.java | 34 +++-- ...eTimeFormatAnnotationFormatterFactory.java | 5 +- .../joda/DateTimeFormatterFactory.java | 130 +++++++++--------- .../joda/DateTimeFormatterFactoryBean.java | 12 +- .../format/datetime/joda/DateTimeParser.java | 6 +- ...eTimeFormatAnnotationFormatterFactory.java | 24 ++-- .../format/datetime/joda/JodaTimeContext.java | 22 +-- .../datetime/joda/JodaTimeContextHolder.java | 5 +- .../datetime/joda/JodaTimeConverters.java | 96 ++++++------- .../joda/JodaTimeFormatterRegistrar.java | 65 +++++---- .../joda/MillisecondInstantPrinter.java | 6 +- .../datetime/joda/ReadableInstantPrinter.java | 7 +- .../datetime/joda/ReadablePartialPrinter.java | 6 +- .../format/datetime/joda/package-info.java | 2 +- .../DateTimeFormatterFactoryBeanTests.java | 3 +- .../joda/DateTimeFormatterFactoryTests.java | 3 +- .../joda/JodaTimeFormattingTests.java | 2 +- 19 files changed, 263 insertions(+), 241 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java b/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java index 8d22560e0f..d2184e735f 100644 --- a/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java +++ b/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java @@ -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"); * you may not use this file except in compliance with the License. @@ -23,22 +23,26 @@ import java.lang.annotation.Target; /** * Declares that a field should be formatted as a date time. - * Supports formatting by style pattern, ISO date time pattern, or custom format pattern string. - * Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code java.long.Long}, or Joda Time fields. - *

- * For style-based formatting, set the {@link #style()} attribute to be the style pattern code. + * + *

Supports formatting by style pattern, ISO date time pattern, or custom format pattern string. + * Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code java.long.Long}, + * Joda-Time value types; and as of Spring 4 and JDK 8, to JSR-310 java.time types too. + * + *

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 '-'. - *

- * For ISO-based formatting, set the {@link #iso()} attribute to be the desired {@link ISO} format, such as {@link ISO#DATE}. -

- * For custom formatting, set the {@link #pattern()} attribute to be the DateTime pattern, such as {@code yyyy/MM/dd hh:mm:ss a}. - *

- * Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient one for your formatting needs). + * + *

For ISO-based formatting, set the {@link #iso()} attribute to be the desired {@link ISO} format, + * such as {@link ISO#DATE}. For custom formatting, set the {@link #pattern()} attribute to be the + * DateTime pattern, such as {@code yyyy/MM/dd hh:mm:ss a}. + * + *

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). + * 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 * @author Juergen Hoeller @@ -51,23 +55,24 @@ public @interface DateTimeFormat { /** * The style pattern to use to format the field. - * Defaults to 'SS' for short date time. - * Set this attribute when you wish to format your field in accordance with a common style other than the default style. + *

Defaults to 'SS' for short date time. Set this attribute when you wish to format + * your field in accordance with a common style other than the default style. */ String style() default "SS"; /** * The ISO pattern to use 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. - * Set this attribute when you wish to format your field in accordance with an ISO date time format. + *

Defaults to {@link ISO#NONE}, indicating this attribute should be ignored. + * Set this attribute when you wish to format your field in accordance with an ISO format. */ ISO iso() default ISO.NONE; /** * The custom pattern to use to format the field. - * Defaults to empty String, indicating no custom pattern String has been specified. - * Set this attribute when you wish to format your field in accordance with a custom date time pattern not represented by a style or ISO format. + *

Defaults to empty String, indicating no custom pattern String has been specified. + * Set this attribute when you wish to format your field in accordance with a custom + * date time pattern not represented by a style or ISO format. */ String pattern() default ""; @@ -78,18 +83,21 @@ public @interface DateTimeFormat { public enum ISO { /** - * The most common ISO Date Format {@code yyyy-MM-dd} e.g. 2000-10-31. + * The most common ISO Date Format {@code yyyy-MM-dd}, + * e.g. 2000-10-31. */ DATE, /** - * The most common ISO Time Format {@code HH:mm:ss.SSSZ} e.g. 01:30:00.000-05:00. + * The most common ISO Time Format {@code HH:mm:ss.SSSZ}, + * e.g. 01:30:00.000-05:00. */ TIME, /** - * The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ} e.g. 2000-10-31 01:30:00.000-05:00. - * The default if no annotation value is specified. + * The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSZ}, + * e.g. 2000-10-31 01:30:00.000-05:00. + *

This is the default if no annotation value is specified. */ DATE_TIME, diff --git a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java index 365800aba8..b5c660f023 100644 --- a/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java +++ b/spring-context/src/main/java/org/springframework/format/datetime/DateFormatter.java @@ -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"); * you may not use this file except in compliance with the License. @@ -46,7 +46,7 @@ public class DateFormatter implements Formatter { private static final Map ISO_PATTERNS; static { - Map formats = new HashMap(); + Map formats = new HashMap(4); formats.put(ISO.DATE, "yyyy-MM-dd"); formats.put(ISO.TIME, "HH:mm:ss.SSSZ"); formats.put(ISO.DATE_TIME, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"); @@ -89,6 +89,15 @@ public class DateFormatter implements Formatter { this.pattern = pattern; } + /** + * Set the ISO format used for this date. + * @param iso the {@link ISO} format + * @since 3.2 + */ + public void setIso(ISO iso) { + this.iso = iso; + } + /** * Set the style to use to format date values. *

If not specified, DateFormat's default style will be used. @@ -112,7 +121,7 @@ public class DateFormatter implements Formatter { *

  • 'F' = Full
  • *
  • '-' = Omitted
  • *