Java Reference
In-Depth Information
Character
Description
k
Hour in day (1-24)
Hour in AM/PM (0-11)
K
Hour in AM/PM (1-12)
h
m
Minute in hour
Second in minute
s
Millisecond
S
General time zone
z
Z
RFC 822 time zone
ISO 8601 time zone
X
The SimpleDateFormat class was created in previous editions of Java, so you
don't have to perform manual translations for a given date.
Note Different date formats are used within different locales, and the Sim-
pleDateFormat class facilitates locale-specific formatting.
To use the class, an instance must be instantiated either by passing a string-based
pattern as an argument to the constructor or by passing no argument to the constructor
at all. The string-based pattern provides a template that should be applied to the given
date and then a string representing the date in the given pattern style is returned. A pat-
tern consists of a number of different characters strung together. Table 4-8 shows the
different characters that can be used within a pattern.
Any of the pattern characters can be placed together in a string and then passed to
the SimpleDateFormat class. If the class is instantiated without passing a pattern,
the pattern can be applied later using the class's applyPattern() method. The ap-
plyPattern() method also comes in handy when you want to change the pattern of
an instantiated SimpleDateFormat object, as seen in the solution to this recipe.
The following excerpts of code demonstrate the application of a pattern:
SimpleDateFormat dateFormatter1 = new
SimpleDateFormat("MMMMM dd yyyy");
dateFormatter1.applyPattern("MM/dd/YY hh:mm:ss");
Search WWH ::




Custom Search