Using Predefined Formats

Version note:

This Date and Time section uses the date and time APIs in the java\.util package. The java\.time APIs, available in the JDK 8 release, provides a comprehensive date and time model that offers significant improvements over the java\.util classes. The java\.time APIs are described in the Date Time trail. The Legacy Date-Time Code page might be of particular interest.

The DateFormat class allows you to format dates and times with predefined styles in a locale-sensitive manner. The sections that follow demonstrate how to use the DateFormat class with 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. The following example formats 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 .

30 juin 2009     fr_FR
30.06.2009       de_DE
Jun 30, 2009     en_US

The preceding code example specified the DEFAULT formatting style. The DEFAULT style is just one of the predefined formatting styles that the DateFormat class provides, as follows:

  • DEFAULT

  • SHORT

  • MEDIUM

  • LONG

  • FULL

The following table shows how dates are formatted for each style with the U.S. and French locales:

Sample Date Formats

StyleU.S. LocaleFrench Locale
DEFAULTJun 30, 200930 juin 2009
SHORT6/30/0930/06/09
MEDIUMJun 30, 200930 juin 2009
LONGJune 30, 200930 juin 2009
FULLTuesday, June 30, 2009mardi 30 juin 2009

Times

Date objects represent both dates and times. Formatting times with the DateFormat class is similar to formatting dates, except that you create the formatter with the getTimeInstance method, as follows:

DateFormat timeFormatter =
    DateFormat.getTimeInstance(DateFormat.DEFAULT, currentLocale);

The table that follows shows the various predefined format styles for the U.S. and German locales:

Sample Time Formats

StyleU.S. LocaleGerman Locale
DEFAULT7:03:47 AM7:03:47
SHORT7:03 AM07:03
MEDIUM7:03:47 AM07:03:07
LONG7:03:47 AM PDT07:03:45 PDT
FULL7:03:47 AM PDT7.03 Uhr PDT

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 the Locale . Here's a quick example:

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

The following table shows the date and time formatting styles for the U.S. and French locales:

Sample Date and Time Formats

StyleU.S. LocaleFrench Locale
DEFAULTJun 30, 2009 7:03:47 AM30 juin 2009 07:03:47
SHORT6/30/09 7:03 AM30/06/09 07:03
MEDIUMJun 30, 2009 7:03:47 AM30 juin 2009 07:03:47
LONGJune 30, 2009 7:03:47 AM PDT30 juin 2009 07:03:47 PDT
FULLTuesday, June 30, 2009 7:03:47 AM PDTmardi 30 juin 2009 07 h 03 PDT