by Dinesh Thakur

We can change the format of the date in two steps: First, we create a formatter with the getDateInstance method. Then, we invoke the format method, which returns a String containing the formatted date.




Date t;

String dt;

DateFormat df;

df = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);

t = new Date();

dt = df.format(t);

System.out.println(dt + " " + currentLocale.toString());

The format of the dates vary with Locale. Since DateFormat is locale sensitive, it takes care of the formatting details for each Locale.

15 Avr 94        fr_FR

15.4.1994        de_DE

15-Apr-94        en_US

The DEFAULT style is one of the predefined formatting styles, The DateFormat class provides following styles :






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


DEFAULT                   15-Apr-94                                15 avr 94

SHORT                        04/15/94                                 15/04/94

MEDIUM                     15-Apr-94                                15avr 94

LONG                          April 15, 1994                         15 avril 1994

FULL                           Friday, April 15, 1994             vendredi, 15 avril 1994




We can even format times with the getTimelnstance method:

DateFormat tf =

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


DEFAULT        3:58:45PM                  15:58:45

SHORT            3:58PM                       15:58

MEDIUM          3:58:45PM                  15:58:45

LONG               3:58:45PM PDT          15:58:45GMT+02:00

FULL               3:58:45oclock PM PDT 15.58Uhr GMT+02:00


Both Dates and Times


To display a date and time in the same String, we 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.

DateFormat fm = 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

DEFAULT       25-Jun-981:32:19 PM                  25 jun 98 22:32:20

SHORT            6/25/981:32 PM                           25/06/9822:32

MEDIUM         25-Jun-98 1:32:19PM                 25 jun 98 22:32:20

LONG              June 25,19981:32:19 PM PDT     25 juin 199822:32:20GMT+02:00

FULL               Thursday, June 25,19981:32:19   jeudi, 25 juin 199822 h 32

  0'clock PM PDT                              GMT+02:00

To create our own customized formats, we can also use the SimpleDateFormat class. When we create a SimpleDateFormat object, we specify a pattern String. The ontents of the pattern String determine the format of the date and time.


Date t;

String s;

SimpleDateFormat df;

df = new SimpleDateFormat(pattern, currentLocale);

t = new Date();

s = df.format(t);

System.out.println(pattern +” ”+ s);

The following table shows the output generated when the U.S. Locale is specified:


Pattern                                                    Output


dd.MM.yy                                                  09.04.98

yyyy.MM.dd G 'at' hh:mm:ss z              1998.04.09AD at 06:15:55PDT

EEE,MMM d, "yy                                     Thu, Apr 9, '98

h:mm a                                                       6:15 PM

H:mm                                                         18:15

H:mm:ss:SSS                                            18:15:55:624

K:mm a,z                                                   6:15PM,PDT

yyyy.MMMMM.dd GGG hh:mm aaa  1998.April09 AD 06:15 PM

The SimpleDateFormat class is locale sensitive. If we instantiate SimpleDateFormat without a Locale parameter, it will format the date and time according to the default Locale. Both the pattern and the Locale determine the format. SimpleDateFormat formats a date and time differently if the Locale varies.

today = new Date();

result = formatter.format(today);

System.out.println("Locale:" + currentLocale.toString());

System.out.println("Result::”+ result);

When the currentLocale is set to different values, the preceding code example generates this output:

Locale: fr_FR

Result: ven 10 avr 98

Locale: de_DE

Result: Fr 10 Apr 98

Locale: en_US

Result: Thu 9 Apr 98

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateFormatExample
    public static void main( String[] args)
    throws Exception
        Date t = Calendar.getInstance().getTime();
        DateFormat shortf =
        SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT );
        DateFormat longf = SimpleDateFormat.getDateInstance(SimpleDateFormat.LONG );
   DateFormat mediumf =SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.MEDIUM, SimpleDateFormat.LONG );
        System.out.println("Short format :"+ shortf.format(t) );
        System.out.println("Long format :"+ longf.format(t) );
        System.out.println("Medium format :"+ mediumf.format(t) );
        String dateAsText = shortf.format(t);
        Date textAsDate = shortf.parse(dateAsText);
        System.out.println( textAsDate );

Date Format Example