DateFormat sample program in Java

By: Manoj Kumar Viewed: 153324 times  Printer Friendly Format    


DateFormat is an abstract class that provides the ability to format and parse dates and times. The getDateInstance() method returns an instance of DateFormat that can format date information. It is available in these forms:

static final DateFormat getDateInstance( )
static final DateFormat getDateInstance(int style)
static final DateFormat getDateInstance(int style, Locale locale)

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the date to be presented. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used.

One of the most commonly used methods in this class is format( ). It has several overloaded forms, one of which is shown here:

String format(Date d)

The argument is a Date object that is to be displayed. The method returns a string containing the formatted information.

The following program illustrates how to format date information. It begins by creating a Dateobject. This captures the current date and time information. Then it outputs the date information by using different styles and locales.

// Demonstrate date formats.
import java.text.*;
import java.util.*;
public class DateFormatDemo {
public static void main(String args[]) {
Date date = new Date();
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: 99/02/19
Korea: 1999-02-19
United Kingdom: 19 February 1999
United States: Friday, February 19, 1999

The getTimeInstance( ) method returns an instance of DateFormat that can format time information. It is available in these versions:

static final DateFormat getTimeInstance( )
static final DateFormat getTimeInstance(int style)
static final DateFormat getTimeInstance(int style, Locale locale)

The argument style is one of the following values: DEFAULT, SHORT, MEDIUM, LONG, or FULL. These are int constants defined by DateFormat. They cause different details about the time to be presented. The argument locale is one of the static references defined by Locale. If the style and/or locale is not specified, defaults are used. The following listing illustrates how to format time information. It begins by creating a Date object. 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);
System.out.println("Canada: " + df.format(date));
}
}

Sample output from this program is shown here:

Japan: 20:25
United Kingdom: 20:25:14 GMT-05:00
Canada: 8:25:14 o'clock PM EST

The DateFormat class also has a getDateTimeInstance( ) method that can format both date and time information. You may wish to experiment with it on your own.

This tutorial is an extract from the "The Complete Reference Part 2 by Herbert Schildt".



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial