Date and time formatting

Format date and time values with text patterns that define the elements of the timestamp.

Patterns can be used for output to define how the timestamp should appear, or input to define the format of the string to convert to a Date/time value.

A typical pattern is "yyyy-MM-dd HH:mm", which creates a formatted timestamp like 2013-03-05 19:52.

Date and time formatting pattern elements

These symbols are the pattern elements that define date/time formats.

Symbol

Meaning

Example pattern

Result

G

Era

G

AD

C

Century of era

C

20

y, Y, or x

Year

y
yy
yyy

2003
03
2003

M

Month

M
MM
MMM
MMMM

1
01
Jan
January

d

Day in month

d
dd

3
03

h

Hour (1-12)

h
hh

7
07

H

Hour (0-23)

H
HH
HH

23
23
00

k

Hour (1-24)

k
kk
kk

3
03
24

K

Hour (0-11)

K
  KK

0
  00

a

AM/PM

a
aa

AM
AM

m

Minute

m
mm

7
07

s

Second

s
ss

2
02

S

Millisecond (0-9999)

SSS
SSSS

007
0070

E

Day in week, name

E
EE
EEE
EEEE

Tue
Tue
Tue
Tuesday

e

Day in week, number (Monday=1)

e
ee
eee

2
02
002

D

Day in year (1-365 or 1-366)

D
DDD

65
065

w

Week in year (1-53)

w

7

z

Time zone

z
zzz
zzzz

PST
PST
Pacific Standard Time

Z

Time zone offset/id

Z
ZZ
ZZZ

-0700
-07:00
America/Los_Angeles

'

Escape for text

'hour' h

hour 9

''

Single quote (x2 single-quote chars)

ss''SSS

45'876

Here are some examples based on the timestamp: 2001-07-04 12:08:56 local time in the U.S. Pacific Time time zone.

Date and Time Pattern

Result

"yyyy.MM.dd G 'at' HH:mm:ss z"

2001.07.04 AD at 12:08:56 PDT

"EEE, MMM d, ''yy"

Wed, Jul 4, '01

"h:mm a"

12:08 PM

"hh 'o''clock' a, zzzz"

12 o'clock PM, Pacific Daylight Time

"K:mm a, z"

0:08 PM, PDT

"yyyyy.MMMMM.dd GGG hh:mm aaa"

02001.July.04 AD 12:08 PM

"EEE, d MMM yyyy HH:mm:ss Z"

Wed, 4 Jul 2001 12:08:56 -0700

"yyMMddHHmmssZ"

010704120856-0700

"yyyy-MM-dd'T'HH:mm:ss.SSSZ"

2001-07-04T12:08:56.235-0700