Java Reference
In-Depth Information
Table 13-2. List of Formatting Symbols for Formatting Date and Time
Letter
Date or Time Component
Presentation
Examples
G
Era designator
Text
AD
y
Year
Year
2003; 03
Y
Week-based year
Year
2003; 03
M
Month in year
Month
March; Mar; 03
w
Week in year
Number
27
W
Week in month
Number
2
D
Day in year
Number
189
d
Day in month
Number
10
F
Day of week in month
Number
2
E
Day in week
Text
Tuesday; Tue
a
AM/PM marker
Text
PM
H
Hour in day (0-23)
Number
0
k
Hour in day (1-24)
Number
24
K
Hour in AM/PM (0-11)
Number
0
h
Hour in AM/PM (1-12)
Number
12
m
Minute in hour
Number
30
s
Second in minute
Number
55
S
Millisecond
Number
978
z
Time zone
General time zone
Pacific Standard Time; PST; GMT-08:00
Z
Time zone
RFC 822 time zone
-0800
You can embed literals inside formatted dates. Suppose you have your birth date (September 19, 1969) stored in
a date object and now you want to print it as “I was born on the Day 19 of the month September in 1969”. Some parts
in the message come from the birth date and others are literals, which are intended to appear in the message as they
are. You cannot use any alphabet's a-z and A-Z as literals inside a date pattern. You need to place them inside single
quotes to treat them as literals, not part of the formatting pattern. First, you need a Date object to represent
September 19, 1969. The Date class' constructor, which takes year, month, and day, has been deprecated. Let's start
with the GregorianCalendar class and use its getTime() method to get a Date object. The following snippet of code
prints this message:
// Create a GregorianCalendar object with September 19, 1969 as date
GregorianCalendar gc = new GregorianCalendar(1969,
Calendar.SEPTEMBER,19);
// Get date object
Date birthDate = gc.getTime();
// Create the pattern. You must place literals inside single quotes
String pattern = "'I was born on the day' dd 'of the month' MMMM 'in' yyyy";
 
Search WWH ::




Custom Search