|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheDateFormatclass allows you to format dates and times with predefined styles in a locale-sensitive manner. The sections that follow demonstrate how to use the
DateFormatclass with a program calledDateFormatDemo.java.
Formatting dates with theDateFormatclass is a two-step process. First, you create a formatter with thegetDateInstancemethod. Second, you invoke theformatmethod, which returns aStringcontaining 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. SinceDateFormatis locale-sensitive, it takes care of the formatting details for eachLocale.9 avr 98 fr_FR 9.4.1998 de_DE 09-Apr-98 en_USThe preceding code example specified the
DEFAULTformatting style. TheDEFAULTstyle is just one of the predefined formatting styles that theDateFormatclass 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
Style U.S. Locale French Locale DEFAULT10-Apr-98 10 avr 98 SHORT4/10/98 10/04/98 MEDIUM10-Apr-98 10 avr 98 LONGApril 10, 1998 10 avril 1998 FULLFriday, April 10, 1998 vendredi, 10 avril 1998
Dateobjects represent both dates and times. Formatting times with theDateFormatclass is similar to formatting dates, except that you create the formatter with thegetTimeInstancemethod, 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
Style U.S. Locale German Locale DEFAULT3:58:45 PM 15:58:45 SHORT3:58 PM 15:58 MEDIUM3:58:45 PM 15:58:45 LONG3:58:45 PM PDT 15:58:45 GMT+02:00 FULL3:58:45 oclock PM PDT 15.58 Uhr GMT+02:00
To display a date and time in the sameString, create the formatter with thegetDateTimeInstancemethod. The first parameter is the date style, and the second is the time style. The third parameter is theLocale. 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
Style U.S. Locale French Locale DEFAULT25-Jun-98 1:32:19 PM 25 jun 98 22:32:20 SHORT6/25/98 1:32 PM 25/06/98 22:32 MEDIUM25-Jun-98 1:32:19 PM 25 jun 98 22:32:20 LONGJune 25, 1998 1:32:19 PM PDT 25 juin 1998 22:32:20 GMT+02:00 FULLThursday, June 25, 1998 1:32:19 o'clock PM PDT jeudi, 25 juin 1998 22 h 32 GMT+02:00
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.