Previous | Next | Trail Map | Internationalization | Date and Time Formatting

Using Predefined Formats

The DateFormat(in the API reference documentation)class allows you to format dates and times with predefined styles in a locale-sensitive manner. Like all locale-sensitive classes, DateFormat does not support all possible Locale definitions. To find out which Locale definitions DateFormat recognizes, invoke the getAvailableLocales method:
Locale[] locales = DateFormat.getAvailableLocales();
In the sections that follow, we'll show you how to format dates and times with the DateFormat class. The examples shown are from a program called DateFormatDemo.java.

Dates

Formatting dates with the DateFormat class is a two-step process. First, you create a formatter with the getDateInstance method. Second, you invoke the format method, which returns a String containing the formatted date. In the following example, we format today's date by calling these two methods:
Date today;
String dateOut;
DateFormat dateFormatter;

dateFormatter = 
   DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
today = new Date();
dateOut = dateFormatter.format(today);

System.out.println(dateOut + "   " + currentLocale.toString());
The output generated by this code follows. Notice that the formats of the dates vary with Locale. Since DateFormat is locale-sensitive, it takes care of the formatting details for each Locale.
9 avr 98   fr_FR
9.4.1998   de_DE
09-Apr-98   en_US
In the preceeding code example, we specified the DEFAULT formatting style. The DEFAULT style is just one of the predefined formatting styles that the DateFormat class provides: The following table shows how dates are formatted for each style with the U.S. and French locales:

Style U.S. Locale French Locale
DEFAULT 10-Apr-98 10 avr 98
SHORT 4/10/98 10/04/98
MEDIUM 10-Apr-98 10 avr 98
LONG April 10, 1998 10 avril 1998
FULL Friday, April 10, 1998 vendredi, 10 avril 1998

Times

Date objects represent both dates and times. Formatting times with the DateFormat class is similar to formatting dates, except you create the formatter with the getTimeInstance method:
DateFormat timeFormatter = 
   DateFormat.getTimeInstance(DateFormat.DEFAULT, currentLocale);
The table that follows shows the different predefined format styles for the U.S. and German locales:

Style U.S. Locale German Locale
DEFAULT 3:58:45 PM 15:58:45
SHORT 3:58 PM 15:58
MEDIUM 3:58:45 PM 15:58:45
LONG 3:58:45 PM PDT 15:58:45 GMT+02:00
FULL 3:58:45 oclock PM PDT 15.58 Uhr GMT+02:00

Both Dates and Times

To display a date and time in the same String, create the formatter with the getDateTimeInstance method. The first parameter is the date style, and the second is the time style. The third parameter is our old friend, the Locale. Here's a quick example:
DateFormat formatter;

formatter = DateFormat.getDateTimeInstance(DateFormat.LONG,
                                           DateFormat.LONG,
                                           currentLocale);

For the U.S. Locale, the formatter in the preceeding code example formats the date and time like this:
April 10, 1998 4:05:54 PM PDT 
If the example formatter is created with the French Locale, the String returned by the format method is:
11 avril 1998 01:05:54 GMT+02:00


Previous | Next | Trail Map | Internationalization | Date and Time Formatting