Creates a formatter using the specified pattern.
This method will create a formatter based on a simple pattern of letters and symbols.
For example,
d MMM yyyy will format 2011-12-03 as '3 Dec 2011'.
The returned formatter will use the default locale, but this can be changed
using
DateTimeFormatter#withLocale(Locale).
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters.
The following pattern letters are defined:
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era number/text 1; 01; AD; Anno Domini
y year year 2004; 04
D day-of-year number 189
M month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q quarter-of-year number/text 3; 03; Q3
Y week-based-year year 1996; 96
w week-of-year number 27
W week-of-month number 27
e localized day-of-week number 2; Tue; Tuesday; T
E day-of-week number/text 2; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;
p pad next pad modifier 1
' escape for text delimiter
'' single quote literal '
[ optional section start
] optional section end
{} reserved for future use
The count of pattern letters determine the format.
Text: The text style is determined based on the number of pattern letters used.
Less than 4 pattern letters will use the
TextStyle#SHORT.
Exactly 4 pattern letters will use the
TextStyle#FULL.
Exactly 5 pattern letters will use the
TextStyle#NARROW.
Number: If the count of letters is one, then the value is printed using the minimum number
of digits and without padding as per
DateTimeFormatterBuilder#appendValue(TemporalField).
Otherwise, the count of digits is used as the width of the output field as per
DateTimeFormatterBuilder#appendValue(TemporalField,int).
Number/Text: If the count of pattern letters is 3 or greater, use the Text rules above.
Otherwise use the Number rules above.
Fraction: Outputs the nano-of-second field as a fraction-of-second.
The nano-of-second value has nine digits, thus the count of pattern letters is from 1 to 9.
If it is less than 9, then the nano-of-second value is truncated, with only the most
significant digits being output.
When parsing in strict mode, the number of parsed digits must match the count of pattern letters.
When parsing in lenient mode, the number of parsed digits must be at least the count of pattern
letters, up to 9 digits.
Year: The count of letters determines the minimum field width below which padding is used.
If the count of letters is two, then a
DateTimeFormatterBuilder#appendValueReducedtwo digit form is used.
For printing, this outputs the rightmost two digits. For parsing, this will parse using the
base value of 2000, resulting in a year within the range 2000 to 2099 inclusive.
If the count of letters is less than four (but not two), then the sign is only output for negative
years as per
SignStyle#NORMAL.
Otherwise, the sign is output if the pad width is exceeded, as per
SignStyle#EXCEEDS_PAD
ZoneId: This outputs the time-zone ID, such as 'Europe/Paris'.
If the count of letters is two, then the time-zone ID is output.
Any other count of letters throws
IllegalArgumentException.
Zone names: This outputs the display name of the time-zone ID.
If the count of letters is one, two or three, then the short name is output.
If the count of letters is four, then the full name is output.
Five or more letters throws
IllegalArgumentException.
Offset X and x: This formats the offset based on the number of pattern letters.
One letter outputs just the hour', such as '+01', unless the minute is non-zero
in which case the minute is also output, such as '+0130'.
Two letters outputs the hour and minute, without a colon, such as '+0130'.
Three letters outputs the hour and minute, with a colon, such as '+01:30'.
Four letters outputs the hour and minute and optional second, without a colon, such as '+013015'.
Five letters outputs the hour and minute and optional second, with a colon, such as '+01:30:15'.
Six or more letters throws
IllegalArgumentException.
Pattern letter 'X' (upper case) will output 'Z' when the offset to be output would be zero,
whereas pattern letter 'x' (lower case) will output '+00', '+0000', or '+00:00'.
Offset Z: This formats the offset based on the number of pattern letters.
One, two or three letters outputs the hour and minute, without a colon, such as '+0130'.
Four or more letters throws
IllegalArgumentException.
The output will be '+0000' when the offset is zero.
Optional section: The optional section markers work exactly like calling
DateTimeFormatterBuilder#optionalStart() and
DateTimeFormatterBuilder#optionalEnd().
Pad modifier: Modifies the pattern that immediately follows to be padded with spaces.
The pad width is determined by the number of pattern letters.
This is the same as calling
DateTimeFormatterBuilder#padNext(int).
For example, 'ppH' outputs the hour-of-day padded on the left with spaces to a width of 2.
Any unrecognized letter is an error.
Any non-letter character, other than '[', ']', '{', '}' and the single quote will be output directly.
Despite this, it is recommended to use single quotes around all characters that you want to
output directly to ensure that future changes do not break your application.