You use one class,
DateFormat
, to format both dates and times.
DateFormat
converts internal date and time data
into text strings for meaningful display. It can:
- Handle timezones and daylight savings adjustments
- Support multiple calendars
- Support European date fields such as the number of the week in the year
- Support definition of formats with string patterns and can retrieve the normalized pattern
- Can parse anything it can format
Let's look at how
the AroundTheWorld
applet uses DateFormat
to
format its current date and time fields and then look at
more advanced formatting of data and time data.
AroundTheWorld
uses the following code
to format the current date:
TimeZone tz = TimeZone.getTimeZone(labels.getString("TimeZone"));
. . .
dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
dateFormatter.setTimeZone(tz);
dateValue.setText(dateFormatter.format(date));
The first line of code gets a
TimeZone
object. TimeZone
s represent a time zone and are used by
the date and time formatters to figure out the current date and time
for the specified zone. So, if you are running AroundTheWorld
in California in the late evening, the date for the France locale is
correctly displayed as tomorrow's date because France is several
hours ahead of California.
The second line of code gets a date formatter for currentLocale
.
DateFormat.DEFAULT
specifies that the date formatter should use
the formatter's default style. DateFormat
provides
four format styles that control the length of the result, SHORT
,
MEDIUM
, LONG
, and FULL
. The default
style is set to MEDIUM
.
Note that the applet called DateFormat
's getDateInstance
.
DateFormat
has other factory methods, such as getInstance
and
getTimeInstance
, that create formatters for formatting dates and times
together or just times. Later in the program AroundTheWorld
uses
getTimeInstance
to get a formatter for formatting the current time.
You'll see this in action in a moment.
The third line sets the formatter's time zone. This ensures that the
date and time fields reflect the current date and time in the time zone
for the displayed locale.
Finaly, the program calls format
to format the date. The format
method returns the
the date formatted in a String
that is then used to
set the Label
's text.
AroundTheWorld
formats the current time in a similar fashion
but uses getTimeInstance
instead of getDateInstance
to get a DateFormat
object appropriate for formatting times:
timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale);
TimeZone tz = TimeZone.getTimeZone(labels.getString("TimeZone"));
timeFormatter.setTimeZone(tz);
. . .
timeValue.setText(timeFormatter.format(date));
Note that the current time is updated every second so the code
for updating the label's text is in a separate method that gets
called by the clock thread.
The two examples shown above are fairly simple--they both use one of
DateFormat
's default styles. For most programmers, the
default styles are good enough. However, other programmers want more
control. DateFormat
provides the ability to format you
own date and time format styles.
Here's another demo program from Taligent
that you can use to get an idea of how to customize date and time
formatting styles.