Java Reference
In-Depth Information
24.5. Formatting and Parsing Dates and Times
Date and time formatting is a separate issue from calendars, although
they are closely related. Formatting is localized in a different way. Not
only are the names of days and months different in different locales that
share the same calendar, but also the order in which a dates' compon-
ents are expressed changes. In the United States it is customary in short
dates to put the month before the date, so that July 5 is written as 7/5.
In many European countries the date comes first, so 5 July becomes 5/7
or 5.7 or …
In the previous sections the word "date" meant a number of milliseconds
since the epoch, which could be interpreted as year, month, day-of-
month, hours, minutes, and seconds information. When dealing with the
formatting classes you must distinguish between
dates,
which deal with
year, month, and day-of-month information, and
times,
which deal with
hours, minutes, and seconds.
Date and time formatting issues are text issues, so the classes for
formatting are in the
java.text
packagethough the
java.util.Formatter
localize its output. If you want localization, you need a
DateFormat
object.
DateFormat
provides several ways to format and parse dates and times. It
page
710
. There are three kinds of formatters, each returned by differ-
ent static methods: date formatters from
geTDateInstance
, time format-
ters from
getTimeInstance
, and date/time formatters from
getdateTimeIn-
stance
. Each of these formatters understands four formatting styles:
SHORT
,
MEDIUM
,
LONG
, and
FULL
, which are constants defined in
DateFormat
.
And for each of them you can either use the default locale or specify one.
For example, to get a medium date formatter in the default locale, you
would use
Format fmt = DateFormat.getDateInstance(DateFormat.MEDIUM);