Java Reference
In-Depth Information
The Locale class also defines static Locale objects that represent languages:
Because the DateFormat class is abstract , you can't create objects of the class directly, but you can
obtain DateFormat objects by using static methods that are defined in the class, each of which returns a
value of type DateFormat . A DateFormat object encapsulates a Locale and an integer date style. The style
is defined by one of the constants defined in the DateFormat class, SHORT , MEDIUM , LONG , or FULL that you
saw earlier.
Each Locale object has a default style that matches conventions that apply for the country or language it
represents.
You can create DateFormat instances that can format a Date object as a time, as a date, or as a date and
a time. The static methods that create DateFormat objects of various kinds are getTimeInstance() that
returns a time formatter, getDateInstance() that returns a date formatter, and getDateTimeInstance()
that returns an object that can format the date and the time. The first two come in three flavors, a no-arg
version where you get a formatter for the default locale and style, a single argument version where you sup-
ply the style for the default locale, and a version that accepts a style argument and a Locale argument. The
getDateTimeInstance() also comes in three versions, the no-arg version that creates a formatter for the
default locale and the default date and time style, a version where you supply two arguments that are the date
style and the time style, and a version that requires three arguments that are the date style, the time style, and
a Locale object for the locale.
When you've obtained a DateFormat object for the country and the style that you want, and the sort of
data you want to format — the date or the time or both — you're ready to produce a String from the Date
object.
You just pass the Date object to the format() method for the DateFormat object. For example:
Date today = new Date(); // Object for now - today's date
DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.FULL, // Date style
DateFormat.FULL, // Time style
Locale.US); // Locale
String formatted = fmt.format(today);
The first statement creates a Date object that represents the instant in time when the call to the Date
constructor executes. The second statement creates a DateFormat object that can format the date and time
encapsulated by a Date object. In this case you specify the formatting style for the data and the time to be
the same, the FULL constant in the DateFormat class. This provides the most detailed specification of the
date and time. The third argument, Locale.US , determines that the formatting should correspond to that re-
quired for the United States. The Locale class defines constants for other major countries and languages.
The third statement applies the format() method of the fmt object to the Date object. After executing these
statements, the String variable formatted contains a full representation of the date and the time when the
Date object today was created.
You can try out some dates and formats in an example.
Search WWH ::




Custom Search