Custom Date Format Patterns
Description
The following list shows the formatting symbols we can use to create custom data format patterns.
- G
Era
Example:G AD GGGG Anno Domini GGGGG A
- u
Year
It can be a positive or negative number. Year is a positive number After an era start date. Year is a negative number before an era start date. 2014 AD is 2014 and 2014 BC is -2014.
Example:u/uuu/uuuu 2014 uu 12 uuuuu 02014
- y
Year of era
It counts the year forward or backward from the era start date. It is always a positive number. 2014 AD is 2014 and 2014 BC is 2015. In Common Era, year 0 is 1 BC.
Example:y/yyy/yyyy 2014 yy 12 yyyyy 02014
- D
Day of year (1 -366)
Example:D 150
- M/L
Month of year
Example:M 5 MM 05 MMM Jul MMMM July
- d
Day of month
Example:
d 21 dd 01 ddd 001 ddd 029
Quarter of year
Example:
Q 3 QQ 03 QQQ Q3 QQQQ 3rd quarter
Week-based year
Example:
Y 2014 YY 12 YYY/YYYY 2014
Week of week-based year
Example:
w 31
Week of month
Example:
W 2
Example:
E 7 EE 07 EEE Sun EEEEE Sunday
Day of week in month
Example:
F 1
AM/PM of day
Example:
a AM
Clock hour of AM/PM (1-12)
Example:
h 2
Hour of AM/PM (0-11)
Example:
K 3
Clock hour of AM/PM (1-24)
Example:
k 7
Hour of day (0-23)
Example:
H 7 HH 07
Minute of hour
Example:
mm 21
Second of minute
Example:
ss 12
Fraction of second
Example:
SSSSSSSSS 000006789
Millisecond of day
Example:
A 12014012
Nanosecond of second
Example:
n 789
Nanosecond of day
Example:
N 12014012001289
Time zone ID
Example:
VV America/Chicago
Time zone name
Example:
z CDT
Zone offset
Example:
Z -0500 ZZ -0500 ZZZ -05:00 ZZZZ GMT-05:00
Localized zone offset
Example:
O GMT-5
Zone offset
Example:
X +09 XX +0930 XXX +09:30 XXX -05:00 XXXX +093045 XXXXX +08:30:45
Same as X, except that it prints '+00' for the zone offset zero, not 'Z'.
Example:
xx -0500
pads the pattern with spaces. The number of p determines the width of the output.
Example:
mm 30 pppmm " 30"
Escape for text.
Text within single quotes is output directly.
To output a single quote, use two consecutive single quotes.
Example:
'Hello' MMMM Hello July
A single quote
Example:
'''Hello''' MMMM 'Hello' July
The symbols [ and ] marks an optional section. A pattern enclosed within an optional section is output only if information is available for all its elements.
reserved for future use.
Example
import java.time.LocalDate;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Locale;
/* w w w . j a va 2 s . c om*/
public class Main {
public static void main(String[] args) {
LocalDate ld = LocalDate.of(2014, Month.JUNE, 30);
format(ld, "M/d/yyyy");
format(ld, "MM/dd/yyyy");
format(ld, "MMM dd, yyyy");
format(ld, "MMMM dd, yyyy");
format(ld, "EEEE, MMMM dd, yyyy");
format(ld, "'Month' q 'in' QQQ");
}
public static void format(Temporal co, String pattern) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
String str = fmt.format(co);
System.out.println(pattern + ": " + str);
}
}
The code above generates the following result.