[an error occurred while processing this directive]

Date and Time Format Example

                         

This Example shows you date and time format according to the locale. In the code given below we are displaying data and time format according to the locale.

Methods used in this example are described below :

DateFormat.getDateInstance() : DateFormat class is used for date and time format and getDateInstance() method is used for gets DateFormat class object with the given formatting style for the given locale.

DateFormat.format() : format method is used for format Date into date/time String.

DateFormatExample.java


import java.text.*;
import java.util.*;

public class DateFormatExample {

    public void displayDate(Locale locale) {

        DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,
                                   locale);
        Date today = new Date();
        String dateOut = dateFormatter.format(today);

        System.out.println(dateOut + "   " + locale.toString());
    }

    public void showDate(Locale locale) {

        Date today = new Date();
        String result;
        DateFormat formatter;

        int[] styles = {DateFormat.DEFAULT, DateFormat.SHORT,
            DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL
        };

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

        for (int k = 0; k < styles.length; k++) {
            formatter = DateFormat.getDateInstance(styles[k], locale);
            result = formatter.format(today);
            System.out.println(result);
        }
    }

    public void showTime(Locale locale) {

        Date today = new Date();
        String result;
        DateFormat formatter;

        int[] styles = {DateFormat.DEFAULT, DateFormat.SHORT,
            DateFormat.MEDIUM, DateFormat.LONG, DateFormat.FULL
        };

        System.out.println();

        for (int k = 0; k < styles.length; k++) {
            formatter = DateFormat.getTimeInstance(styles[k], locale);
            result = formatter.format(today);
            System.out.println(result);
        }
    }

    public static void main(String args[]) {

        Locale[] locales = new Locale[]{new Locale("fr""FR")new Locale("en""IN")};

        DateFormatExample[] dateFormat = new DateFormatExample[locales.length];

        for (int i = 0; i < locales.length; i++) {
            dateFormat[inew DateFormatExample();
            dateFormat[i].displayDate(locales[i]);
        }

        for (int i = 0; i < locales.length; i++) {
            dateFormat[i].showDate(locales[i]);
            dateFormat[i].showTime(locales[i]);
        }
    }
}

Output :

10 sept. 2008   fr_FR
10 Sep, 2008   en_IN

Locale: fr_FR

10 sept. 2008
10/09/08
10 sept. 2008
10 septembre 2008
mercredi 10 septembre 2008

11:28:02
11:28
11:28:02
11:28:02 IST
11 28 IST

Locale: en_IN

10 Sep, 2008
10/9/08
10 Sep, 2008
10 September, 2008
Wednesday, 10 September, 2008

11:28:02 AM
11:28 AM
11:28:02 AM
11:28:02 AM IST
11:28:02 AM IST

Download code

                         

[an error occurred while processing this directive]