Java Reference
In-Depth Information
Symbol
Data
Example
Year
yy : 12
yyyy : 2012
y
MM : 01
MM : 10
MMM : Oct
MMMM : October
M
Month
d : 1
d : 31
dd : 01
d
Day of month
H : 1
H : 23
HH : 01
H
Hour (0-23)
h : 1
h : 12
h
Hour (0-12)
m : 1
m : 59
mm : 01
m
Minutes
s : 1
s : 59
ss : 01
s
Seconds
a : PM
a
AM/PM
10.
Add the following two statements to display the date and time in the defined patterns:
System.out.println(sdf.format(d));
System.out.println(stf.format(d));
11.
Save the DateTimeApp source code and run as an application.
The results should be the current date and time in the default format and the two new patterns:
Thu Feb 17 14:37:56 EST 2011
02/17/2011
02:37:56 PM
12.
Comment out the statements added in step 10.
To set a date such as a due date or birth date, you would first create a Calendar object and then set its month, day,
and year. You can then use the Calendar object's getTime method to retrieve a Date object with the defined date.
Search WWH ::




Custom Search