@Documented @Retention(value=RUNTIME) @Target(value={METHOD,FIELD,PARAMETER,ANNOTATION_TYPE}) public @interface DateTimeFormat
Supports formatting by style pattern, ISO date time pattern, or custom format pattern string.
Can be applied to Date
, Calendar
, Long
(for
millisecond timestamps) as well as JSR-310 java.time
and Joda-Time value types.
For style-based formatting, set the style()
attribute to the desired 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.
The date or time may be omitted by specifying the style character '-' — for example,
'M-' specifies a medium format for the date with no time.
For ISO-based formatting, set the iso()
attribute to the desired DateTimeFormat.ISO
format,
such as DateTimeFormat.ISO.DATE
.
For custom formatting, set the pattern()
attribute to a date time pattern, such as
"yyyy/MM/dd hh:mm:ss a"
.
Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient for your formatting needs).
iso()
attribute is specified, it takes precedence over the style attribute.Whenever the style()
or pattern()
attribute is used, the
default time zone of the JVM will
be used when formatting Date
values. Whenever the iso()
attribute is used when formatting Date
values, UTC
will be used as the time zone. The same time zone will be applied to any
fallback patterns as well. In order to enforce
consistent use of UTC
as the time zone, you can bootstrap the JVM with
-Duser.timezone=UTC
.
DateTimeFormatter
,
DateTimeFormat
Modifier and Type | Optional Element and Description |
---|---|
String[] |
fallbackPatterns
|
DateTimeFormat.ISO |
iso
The ISO pattern to use to format the field or method parameter.
|
String |
pattern
The custom pattern to use to format the field or method parameter.
|
String |
style
The style pattern to use to format the field or method parameter.
|
public abstract String style
Defaults to 'SS' for short date, short time. Set this attribute when you wish to format your field or method parameter in accordance with a common style other than the default style.
fallbackPatterns()
public abstract DateTimeFormat.ISO iso
Supported ISO patterns are defined in the DateTimeFormat.ISO
enum.
Defaults to DateTimeFormat.ISO.NONE
, indicating this attribute should be ignored.
Set this attribute when you wish to format your field or method parameter
in accordance with an ISO format.
fallbackPatterns()
public abstract String pattern
Defaults to empty String, indicating no custom pattern String has been specified. Set this attribute when you wish to format your field or method parameter in accordance with a custom date time pattern not represented by a style or ISO format.
Note: This pattern follows the original SimpleDateFormat
style,
as also supported by Joda-Time, with strict parsing semantics towards overflows
(e.g. rejecting a Feb 29 value for a non-leap-year). As a consequence, 'yy'
characters indicate a year in the traditional style, not a "year-of-era" as in the
DateTimeFormatter
specification (i.e. 'yy' turns into 'uu'
when going through a DateTimeFormatter
with strict resolution mode).
fallbackPatterns()
public abstract String[] fallbackPatterns
pattern()
, iso()
, or style()
attribute.
For example, if you wish to use the ISO date format for parsing and printing but allow for lenient parsing of user input for various date formats, you could configure something similar to the following.
@DateTimeFormat(iso = ISO.DATE, fallbackPatterns = { "M/d/yy", "dd.MM.yyyy" })
Fallback patterns are only used for parsing. They are not used for
printing the value as a String. The primary pattern()
, iso()
,
or style()
attribute is always used for printing. For details on
which time zone is used for fallback patterns, see the
class-level documentation.
Fallback patterns are not supported for Joda-Time value types.