Locale-Sensitive Data |
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:Let's look at how the
- 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
AroundTheWorld
applet usesDateFormat
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:The first line of code gets aTimeZone tz = TimeZone.getTimeZone(labels.getString("TimeZone")); . . . dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale); dateFormatter.setTimeZone(tz); dateValue.setText(dateFormatter.format(date));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 runningAroundTheWorld
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
, andFULL
. The default style is set toMEDIUM
.Note that the applet called
DateFormat
'sgetDateInstance
.DateFormat
has other factory methods, such asgetInstance
andgetTimeInstance
, that create formatters for formatting dates and times together or just times. Later in the programAroundTheWorld
usesgetTimeInstance
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. Theformat
method returns the the date formatted in aString
that is then used to set theLabel
's text.
AroundTheWorld
formats the current time in a similar fashion but usesgetTimeInstance
instead ofgetDateInstance
to get aDateFormat
object appropriate for formatting times: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.timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale); TimeZone tz = TimeZone.getTimeZone(labels.getString("TimeZone")); timeFormatter.setTimeZone(tz); . . . timeValue.setText(timeFormatter.format(date));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.
The 1.1 Date text box is the result of using
DateFormat
to format the data in the millis text box (the date and time when the demo program was first invoked). The Pattern is used to format the date and time. You can control the pattern using the radio buttons and pulldown menus in the lower right of the demo program window.Here's a guide from Taligent that points out interesting things you can do with the demo program. Experiment with the various settings in the demo program according to Taligent's instructions.
Here's a snippet of Java code that yields the same results as the demo program when it's first invoked:
The first three lines of code set up a Date object with the current time and aCalendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); TimeZone tz = TimeZone.getTimeZone("ECT"); DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US); formatter.setTimeZone(tz); String result = formatter.format(date); System.out.println(result);TimeZone
object that represents the time zone for Paris. In the demo program, the time zone is set with the City pulldown menu. So the current date and time in Paris will be displayed.The fourth line of code gets a
DateFormat
object. This formatter will include the date and time in the result. (In the demo program this is controlled by the Date and Time Format radio button, in the code it's controlled by thegetDateTimeInstance
factory method). This formatter will use the LONG styles for formatting the date and the time (corresponding to the Date Style and Time Style pulldown menus in the demo program). In addition, the formatter is set up to format the date and time in a manner appropriate for the United States Locale (the Locale pulldown menu).Then the code sets the time zone on the formatter, formats the result, and displays it.
Tweaking the settings in the demo applet corresponds to changing this basic Java code in various ways. The following table shows the relationship between the demo programs user interface elements and the number formatter's API.
GUI Element Java Code Equivalent Class getInstance
,getDateTimeInstance
DateFormat
getDateInstance
DateFormat
getTimeInstance
DateFormat
getXxxxxInstance(Locale)
DateFormat
getDateInstance(dateStyle)
,getDateInstance(dateStyle, Locale)
,getDateTimeInstance(dateStyle, timeStyle)
,getDateInstance(dateStyle, timeStyle, Locale)
DateFormat
getTimeInstance(timeStyle)
,getTimeInstance(timeStyle, Locale)
,getDateTimeInstance(dateStyle, timeStyle)
,getDateInstance(dateStyle, timeStyle, Locale)
DateFormat
setTimeZone
DateFormat
setLenient
DateFormat
applyPattern
SimpleDateFormat
applyLocalizedPattern
SimpleDateFormat
add
,roll
Calendar
Let's do an example. Taligent's guide suggests that you do the following in the demo program:
After you've done this, the formatter is no longer using one of its pre-programmed styles. Instead, it's using a user-customized pattern. Here's the Java code that gives the same result:
- Select the Date Format and the English (U.S.) locale
- In the Pattern field, delete an "M"--the month changes to the abbreviated format
- Delete another "M"--the month changes to the numeric format
The bold areas of the code mark the differences between this code segment and the previous. TheCalendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); TimeZone tz = TimeZone.getTimeZone("ECT");SimpleDateFormat formatter =(SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US); formatter.setTimeZone(tz);formatter.applyPattern("MM d, yyyy h:mm:ss a z"); String result = formatter.format(date); System.out.println(result);DateFormat
had to be changed to aSimpleDateFormat
because theDateFormat
API does not support patterns. Also, a line of code was added to set the formatter's pattern before formatting the date. This table lists the field specifications that you can use when specifying a pattern for aSimpleDateFormat
.
This page incorporates material or code copyrighted by Taligent, Inc. For more information on international resources, see their International Fact Sheet.
Locale-Sensitive Data |