|
26 | 26 | * Declares that a field or method parameter should be formatted as a date or time.
|
27 | 27 | *
|
28 | 28 | * <p>Supports formatting by style pattern, ISO date time pattern, or custom format pattern string.
|
29 |
| - * Can be applied to {@code java.util.Date}, {@code java.util.Calendar}, {@code Long} (for |
30 |
| - * millisecond timestamps) as well as JSR-310 <code>java.time</code> and Joda-Time value types. |
| 29 | + * Can be applied to {@link java.util.Date}, {@link java.util.Calendar}, {@link Long} (for |
| 30 | + * millisecond timestamps) as well as JSR-310 {@code java.time} and Joda-Time value types. |
31 | 31 | *
|
32 |
| - * <p>For style-based formatting, set the {@link #style} attribute to be the style pattern code. |
| 32 | + * <p>For style-based formatting, set the {@link #style} attribute to the desired style pattern code. |
33 | 33 | * The first character of the code is the date style, and the second character is the time style.
|
34 | 34 | * Specify a character of 'S' for short style, 'M' for medium, 'L' for long, and 'F' for full.
|
35 |
| - * A date or time may be omitted by specifying the style character '-'. |
| 35 | + * The date or time may be omitted by specifying the style character '-' — for example, |
| 36 | + * 'M-' specifies a medium format for the date with no time. |
36 | 37 | *
|
37 |
| - * <p>For ISO-based formatting, set the {@link #iso} attribute to be the desired {@link ISO} format, |
38 |
| - * such as {@link ISO#DATE}. For custom formatting, set the {@link #pattern} attribute to be the |
39 |
| - * DateTime pattern, such as {@code "yyyy/MM/dd hh:mm:ss a"}. |
| 38 | + * <p>For ISO-based formatting, set the {@link #iso} attribute to the desired {@link ISO} format, |
| 39 | + * such as {@link ISO#DATE}. |
| 40 | + * |
| 41 | + * <p>For custom formatting, set the {@link #pattern} attribute to a date time pattern, such as |
| 42 | + * {@code "yyyy/MM/dd hh:mm:ss a"}. |
40 | 43 | *
|
41 | 44 | * <p>Each attribute is mutually exclusive, so only set one attribute per annotation instance
|
42 | 45 | * (the one most convenient for your formatting needs).
|
|
48 | 51 | * with a style code of 'SS' (short date, short time).</li>
|
49 | 52 | * </ul>
|
50 | 53 | *
|
| 54 | + * <h3>Time Zones</h3> |
| 55 | + * <p>Whenever the {@link #style} or {@link #pattern} attribute is used, the |
| 56 | + * {@linkplain java.util.TimeZone#getDefault() default time zone} of the JVM will |
| 57 | + * be used when formatting {@link java.util.Date} values. Whenever the {@link #iso} |
| 58 | + * attribute is used when formatting {@link java.util.Date} values, {@code UTC} |
| 59 | + * will be used as the time zone. The same time zone will be applied to any |
| 60 | + * {@linkplain #fallbackPatterns fallback patterns} as well. In order to enforce |
| 61 | + * consistent use of {@code UTC} as the time zone, you can bootstrap the JVM with |
| 62 | + * {@code -Duser.timezone=UTC}. |
| 63 | + * |
51 | 64 | * @author Keith Donald
|
52 | 65 | * @author Juergen Hoeller
|
| 66 | + * @author Sam Brannen |
53 | 67 | * @since 3.0
|
54 | 68 | * @see java.time.format.DateTimeFormatter
|
55 | 69 | * @see org.joda.time.format.DateTimeFormat
|
|
60 | 74 | public @interface DateTimeFormat {
|
61 | 75 |
|
62 | 76 | /**
|
63 |
| - * The style pattern to use to format the field. |
64 |
| - * <p>Defaults to 'SS' for short date time. Set this attribute when you wish to format |
65 |
| - * your field in accordance with a common style other than the default style. |
| 77 | + * The style pattern to use to format the field or method parameter. |
| 78 | + * <p>Defaults to 'SS' for short date, short time. Set this attribute when you |
| 79 | + * wish to format your field or method parameter in accordance with a common |
| 80 | + * style other than the default style. |
| 81 | + * @see #fallbackPatterns |
66 | 82 | */
|
67 | 83 | String style() default "SS";
|
68 | 84 |
|
69 | 85 | /**
|
70 |
| - * The ISO pattern to use to format the field. |
71 |
| - * <p>The possible ISO patterns are defined in the {@link ISO} enum. |
| 86 | + * The ISO pattern to use to format the field or method parameter. |
| 87 | + * <p>Supported ISO patterns are defined in the {@link ISO} enum. |
72 | 88 | * <p>Defaults to {@link ISO#NONE}, indicating this attribute should be ignored.
|
73 |
| - * Set this attribute when you wish to format your field in accordance with an ISO format. |
| 89 | + * Set this attribute when you wish to format your field or method parameter |
| 90 | + * in accordance with an ISO format. |
| 91 | + * @see #fallbackPatterns |
74 | 92 | */
|
75 | 93 | ISO iso() default ISO.NONE;
|
76 | 94 |
|
77 | 95 | /**
|
78 |
| - * The custom pattern to use to format the field. |
79 |
| - * <p>Defaults to empty String, indicating no custom pattern String has been specified. |
80 |
| - * Set this attribute when you wish to format your field in accordance with a custom |
81 |
| - * date time pattern not represented by a style or ISO format. |
| 96 | + * The custom pattern to use to format the field or method parameter. |
| 97 | + * <p>Defaults to empty String, indicating no custom pattern String has been |
| 98 | + * specified. Set this attribute when you wish to format your field or method |
| 99 | + * parameter in accordance with a custom date time pattern not represented by |
| 100 | + * a style or ISO format. |
82 | 101 | * <p>Note: This pattern follows the original {@link java.text.SimpleDateFormat} style,
|
83 | 102 | * as also supported by Joda-Time, with strict parsing semantics towards overflows
|
84 | 103 | * (e.g. rejecting a Feb 29 value for a non-leap-year). As a consequence, 'yy'
|
85 | 104 | * characters indicate a year in the traditional style, not a "year-of-era" as in the
|
86 | 105 | * {@link java.time.format.DateTimeFormatter} specification (i.e. 'yy' turns into 'uu'
|
87 |
| - * when going through that {@code DateTimeFormatter} with strict resolution mode). |
| 106 | + * when going through a {@code DateTimeFormatter} with strict resolution mode). |
| 107 | + * @see #fallbackPatterns |
88 | 108 | */
|
89 | 109 | String pattern() default "";
|
90 | 110 |
|
| 111 | + /** |
| 112 | + * The set of custom patterns to use as a fallback in case parsing fails for |
| 113 | + * the primary {@link #pattern}, {@link #iso}, or {@link #style} attribute. |
| 114 | + * <p>For example, if you wish to use the ISO date format for parsing and |
| 115 | + * printing but allow for lenient parsing of user input for various date |
| 116 | + * formats, you could configure something similar to the following. |
| 117 | + * <pre style="code"> |
| 118 | + * {@literal @}DateTimeFormat(iso = ISO.DATE, fallbackPatterns = { "M/d/yy", "dd.MM.yyyy" }) |
| 119 | + * </pre> |
| 120 | + * <p>Fallback patterns are only used for parsing. They are not used for |
| 121 | + * printing the value as a String. The primary {@link #pattern}, {@link #iso}, |
| 122 | + * or {@link #style} attribute is always used for printing. For details on |
| 123 | + * which time zone is used for fallback patterns, see the |
| 124 | + * {@linkplain DateTimeFormat class-level documentation}. |
| 125 | + * <p>Fallback patterns are not supported for Joda-Time value types. |
| 126 | + * @since 5.3.5 |
| 127 | + */ |
| 128 | + String[] fallbackPatterns() default {}; |
| 129 | + |
91 | 130 |
|
92 | 131 | /**
|
93 | 132 | * Common ISO date time format patterns.
|
94 | 133 | */
|
95 | 134 | enum ISO {
|
96 | 135 |
|
97 | 136 | /**
|
98 |
| - * The most common ISO Date Format {@code yyyy-MM-dd}, |
99 |
| - * e.g. "2000-10-31". |
| 137 | + * The most common ISO Date Format {@code yyyy-MM-dd} — for example, |
| 138 | + * "2000-10-31". |
100 | 139 | */
|
101 | 140 | DATE,
|
102 | 141 |
|
103 | 142 | /**
|
104 |
| - * The most common ISO Time Format {@code HH:mm:ss.SSSXXX}, |
105 |
| - * e.g. "01:30:00.000-05:00". |
| 143 | + * The most common ISO Time Format {@code HH:mm:ss.SSSXXX} — for example, |
| 144 | + * "01:30:00.000-05:00". |
106 | 145 | */
|
107 | 146 | TIME,
|
108 | 147 |
|
109 | 148 | /**
|
110 |
| - * The most common ISO DateTime Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSXXX}, |
111 |
| - * e.g. "2000-10-31T01:30:00.000-05:00". |
| 149 | + * The most common ISO Date Time Format {@code yyyy-MM-dd'T'HH:mm:ss.SSSXXX} |
| 150 | + * — for example, "2000-10-31T01:30:00.000-05:00". |
112 | 151 | */
|
113 | 152 | DATE_TIME,
|
114 | 153 |
|
|
0 commit comments