java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get default date and time format for a defined country?

The DateFormat class allows you to format dates and times with predefined styles in a locale-sensitive manner. Formatting dates or times with the DateFormat class is a two-step process.

First, you create a formatter with the getDateInstance() method for formatting date or getTimeInstance() method for formatting time or getDateTimeInstance() when you want formatting both date and time.

Second, you invoke the format method, which returns a String containing the formatted date. The following example formats today's date and time by calling those two methods.

package com.javacoderanch.example.text;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

public class LocaleDateTime {
    public static void main(String[] args) {
        Locale[] locales = {
        Date today = new Date();

        for (Locale locale : locales) {
            StringBuilder sb = new StringBuilder();

            // Gets a DateFormat instance for the specified locale
            // and format a date object by calling the format
            // method.
            DateFormat df = DateFormat.getDateInstance(
                    DateFormat.DEFAULT, locale);
            String date = df.format(today);
            sb.append("n Default date format: ").append(date);

            // Gets a DateFormat instance for the specified locale
            // and format a time information by calling the format
            // method.
            DateFormat tf = DateFormat.getTimeInstance(
                    DateFormat.DEFAULT, locale);
            String time = tf.format(today.getTime());
            sb.append("n Default time format: ").append(time)


        // Gets date and time formatted value for Italy locale using
        // To display a date and time in the same String, 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 dtf = DateFormat.getDateTimeInstance(
                DateFormat.DEFAULT, DateFormat.DEFAULT,
        String datetime = dtf.format(today);

        System.out.println("date time format in " +
                Locale.ITALY.getDisplayCountry() + ": " + datetime);

Here are the produces output:

 Default date format: 10-Sep-2010
 Default time format: 9:37:58 AM

 Default date format: 10 sept. 2010
 Default time format: 09:37:58

 Default date format: 10.09.2010
 Default time format: 09:37:58

United States
 Default date format: Sep 10, 2010
 Default time format: 9:37:58 AM

 Default date format: 2010/09/10
 Default time format: 9:37:58

date time format in Italy: 10-set-2010 9.37.58