Chapter 27: NIO, Regular Expressions, and Other Packages 841
DateFormat df;
df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.JAPAN);
System.out.println("Japan: " + df.format(date));
df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.KOREA);
System.out.println("Korea: " + df.format(date));
df = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK);
System.out.println("United Kingdom: " + df.format(date));
df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
System.out.println("United States: " + df.format(date));
}
}
Sample output from this program is shown here:
Japan: 06/07/12
Korea: 2006. 7. 12
United Kingdom: 12 July 2006
United States: Wednesday, July 12, 2006
ThegetTimeInstance( )method returns an instance ofDateFormatthat can format time
information. It is available in these versions:
static final DateFormat getTimeInstance( )
static final DateFormat getTimeInstance(intstyle)
static final DateFormat getTimeInstance(intstyle, Localelocale)
The argumentstyleis one of the following values:DEFAULT,SHORT,MEDIUM,LONG,
orFULL. These areintconstants defined byDateFormat. They cause different details about
the time to be presented. The argumentlocaleis one of the static references defined byLocale.
If thestyleand/orlocaleis not specified, defaults are used.
The following listing illustrates how to format time information. It begins by creating a
Dateobject. This captures the current date and time information and then outputs the time
information by using different styles and locales.
// Demonstrate time formats.
import java.text.;
import java.util.;
public class TimeFormatDemo {
public static void main(String args[]) {
Date date = new Date();
DateFormat df;
df = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.JAPAN);
System.out.println("Japan: " + df.format(date));
df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.UK);
System.out.println("United Kingdom: " + df.format(date));
df = DateFormat.getTimeInstance(DateFormat.FULL, Locale.CANADA);