DateTimeFormatter class ofPattern() method returns a DateTimeFormatter object with the specified format pattern and locale.
static DateTimeFormatter ofPattern(String pattern) static DateTimeFormatter ofPattern(String pattern, Locale locale)
The following Code creates two formatters to format a date in "Month day, Year" format.
The first formatter formats the datetime in the default locale and the second one in the German locale.
//Get a formatter for the default locale DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("MMMM dd, yyyy"); // Get a formatter for the German locale DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.GERMAN);
DateTimeFormatter class withLocale() method returns a DateTimeFormatter object for the specified locale that uses the same pattern.
You could replace the second statement with the following one:
// Get a formatter for the German locale using the same pattern as fmt1
DateTimeFormatter fmt2 = fmt1.withLocale(Locale.GERMAN);
A formatting pattern is a sequence of characters that have special meanings.
For example,
All letters 'A' to 'Z' and 'a' to 'z' are reserved as pattern letters, although not all are used.
To include a literal string in a pattern, you need to enclose it in single quotes.
To output a single quote, you need to use two consecutive single quotes.
A datetime formatter outputs any non-letter characters, other than [, ] and a single quote, directly.
The following table lists the symbols used in patterns and their meanings.
Symbol | Description | Pattern | Output |
---|---|---|---|
G | Era | G GGGG GGGGG | AD Anno Domini A |
u | Year It can be a positive or negative number. After an era start date, it is a positive number. Before an era start date, it is a negative number. For example, the year value for 2018 AD is 2018 and the year value for 2018 BC is -2018. | u/uuu/uuuu uu uuuuu | 2018 12 02018 |
y | Year of era It counts the year forward or backward from the era start date. It is always a positive number. In Common Era, year 0 is 1 BC. | y/yyy/yyyy yy yyyyy | 2018 12 02018 |
D | Day of year (1 -366) | D | 150 |
M/L | Month of year | M MM MMM MMMM | 5 05 Jul July |
d | Day of month The pattern d will output values like 1, 2, 10, 29, etc. The pattern dd will zero-pad the values as 01, 02, 10, 29, etc. to make them always two digits. The pattern ddd will output values like 001, 010, 029, etc. | d | 29 |
Q/q | Quarter of year | Q QQQ QQQQ | 3 03 Q3 3rd quarter |
Y | Week-based year | Y YY YYY/YYYY | 2018 12 2018 |
w | Week of week-based year | w | 31 |
W | Week of month | W | 5 |
E | Day of week | E EE EEE EEEEE | 7 07 Sun Sunday |
F | Day of week in month | F | 1 |
a | AM/PM of day | a | AM |
h | Clock hour of AM/PM (1-12) | h | 7 |
K | Hour of AM/PM (0-11) | K | 7 |
k | Clock hour of AM/PM (1-24) | k | 7 |
H | Hour of day (0-23) | H HH | 7 07 |
m | Minute of hour | mm | 30 |
s | Second of minute | ss | 12 |
S | Fraction of second | SSSSSSSSS | 000000789 |
A | Millisecond of day | A | 27012000 |
n | Nanosecond of second | n | 789 |
N | Nanosecond of day | N | 27012000000789 |
V | Time zone ID | VV | America/Chicago |
z | Time zone name | z | CDT |
Z | Zone offset When the zone offset is zero, it outputs '+0000' or '+00:00' depending on whether you use Z, ZZ, or ZZZ. | Z ZZ ZZZ ZZZZ | -0500 -0500 -05:00 GMT-05:00 |
O | Localized zone offset | O | GMT-5 |
X | Zone offset It prints Z for the zone offset Zero. X outputs only the hour such as '+09'; XX outputs the hour and minute, without a colon such as '+0830'; XXX outputs the hour and minute with a colon such as '+08:30'; XXXX outputs the hour, minute, and optional second, without a colon such as '+083045'; XXXXX outputs the hour, minute, and option second, with a colon such as '+08:30:45'. | XXX | -05:00 |
x | Same as X, except that it prints '+00' for the zone offset zero, not 'Z'. | xx | -0500 |
p | Pad next It pads the output of the pattern following it with spaces. For example, if mm outputs 30, pppmm will output ' 30' and ppppmm will output ' 30'. The number of p determines the width of the output. | pppmm | ' 30' |
' | Escape for text Text within single quotes is output directly. To output a single quote, use two consecutive single quotes. | 'Hello' 'Hello' MMMM | Hello Hello July |
'' | A single quote | '''Hello''' MMMM | 'Hello' July |
[ ] | An optional section | n/a | n/a |
#, {, } | They are reserved for future use. | n/a | n/a |