Java DateTimeFormatter patterns

Introduction

The following table lists the symbols used in DateTimeFormatter patterns and their meanings.

Symbol
Description
Pattern
Output
G


Era


G
GGGG
GGGGG
AD
Anno Domini
A
u




Year
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 2020 AD
is 2020 and the year value for 2020 BC is -2020.

u/uuu/uuuu
uu
uuuuu


2020
12
02020

y



Year of era
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

2020
12
02020

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


d
dd
ddd
29
01
001
Q/q



Quarter of year



Q
QQ
QQQ
QQQQ
3
03
Q3
3rd quarter
Y


Week-based year


Y
YY
YYY/YYYY
2020
12
2020
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
XX
XXX
XXX
XXXX
XXXXX
'+09'
'+0830'
'+08:30';
-05:00
'+083045'
'+08:30:45'
x

Same as X, except that it prints '+00' for the zone offset
zero, not 'Z'.
xx

-0500

p

Pad next
pads the output of the pattern following it with spaces.
pppmm

' 30'

'

Escape for text

'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
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    // A pattern with an optional section
    String pattern = "MM/dd/yyyy[ 'at' HH:mm:ss]";
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);


    LocalDateTime ldt = LocalDateTime.now();

    String str1 = fmt.format(ldt);
    System.out.println(str1);/*from w w w . j a va 2 s. com*/

    LocalDate ld = LocalDate.now();
    System.out.println("Formatting date: " + ld);
    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");
    format(ld, "[MM-dd-yyyy][' at' HH:mm:ss]");

    LocalTime lt = LocalTime.now();
    System.out.println("\nFormatting time:" + lt);
    format(lt, "HH:mm:ss");
    format(lt, "KK:mm:ss a");
    format(lt, "[MM-dd-yyyy][' at' HH:mm:ss]");

    ZonedDateTime zdt = ZonedDateTime.now();
    System.out.println("\nFormatting zoned datetime:" + zdt);
    format(zdt, "MM/dd/yyyy HH:mm:ssXXX");
    format(zdt, "MM/dd/yyyy VV");
    format(zdt, "[MM-dd-yyyy][' at' HH:mm:ss]");
  }

  public static void format(Temporal co, String pattern) {
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, Locale.US);
    String str = fmt.format(co);
    System.out.println(pattern + ": " + str);
  }
}



PreviousNext

Related