You use one class,
DateFormat, to format both dates and times.
DateFormatconverts 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
DateFormatto format its current date and time fields and then look at more advanced formatting of data and time data.
AroundTheWorlduses 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));
TimeZones 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
AroundTheWorldin 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
DateFormat.DEFAULTspecifies that the date formatter should use the formatter's default style.
DateFormatprovides four format styles that control the length of the result,
FULL. The default style is set to
Note that the applet called
DateFormathas other factory methods, such as
getTimeInstance, that create formatters for formatting dates and times together or just times. Later in the program
getTimeInstanceto 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
formatto format the date. The
formatmethod returns the the date formatted in a
Stringthat is then used to set the
AroundTheWorldformats the current time in a similar fashion but uses
getDateInstanceto get a
DateFormatobject 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.
DateFormatprovides 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
DateFormatto 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);
TimeZoneobject 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
DateFormatobject. 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 the
getDateTimeInstancefactory 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
getDateInstance(dateStyle, timeStyle, Locale)
getDateInstance(dateStyle, timeStyle, Locale)
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 formatThe 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");
SimpleDateFormatformatter = (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);
DateFormathad to be changed to a
DateFormatAPI 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 a
This page incorporates material or code copyrighted by Taligent, Inc. For more information on international resources, see their International Fact Sheet.