Java DateFormat Format Examples

0

DateFormat is an abstract class that provides methods for formatting and parsing dates for any locale. If locale is not provided, it will use the default locale.

DateFormat depends on both Locale and something called formatting style. The supported styles are DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, and DateFormat.SHORT. In this article, we will see many examples of formatting for different styles and locales.

DateFormat Instance

By default, locale is default locale returned by Locale.getDefault() and style is SHORT.

  1. DateFormat.getInstance() returns the default DateFormat instance that supports both date and time and it uses DateFormat.SHORT.
  2. DateFormat.getDateInstance() returns the DateFormat instance containing only the date component for the default locale. It has overloaded versions which take style and Locale as additional arguments.
  3. DateFormat.getTimeInstance() returns the DateFormat instance that can be used only for processing time for a default locale. Again it too has overloaded methods that take style and Locale as additional arguments.
  4. DateFormat.getDateTimeInstance() returns the DateFormat instance which can be used for processing both date and time for a default locale. The overloaded versions take
    style and Locale as additional arguments

In our first example, we will use the default DateFormat returned by the default DateFormat.getInstance() and the other date/time specific factory methods like DateFormat.getDateInstance(), DateFormat.getTimeInstance() and DateFormat.getDateTimeInstance().

DefaultDateFormatExample:

package com.javarticles.date;

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


public class DefaultDateFormatExample {
    public static void main(String[] args) {
        DateFormat dateFormat= DateFormat.getInstance();
        System.out.println("Print date using default DateFormat instance, DateFormat.getInstance()");
        printDate(dateFormat);
        
        DateFormat onlyDateFormat = DateFormat.getDateInstance();
        System.out.println("Print date using just the Date Instance, DateFormat.getDateInstance()");
        printDate(onlyDateFormat);
        
        DateFormat onlyTimeFormat = DateFormat.getTimeInstance();
        System.out.println("Print date using just the time Instance, DateFormat.getTimeInstance()");
        printDate(onlyTimeFormat);
        
        DateFormat dateTimeFormat = DateFormat.getDateTimeInstance();
        System.out.println("Print date using just the dateTime Instance, DateFormat.getDateTimeInstance()");
        printDate(dateTimeFormat);
    }
    
    private static void printDate(DateFormat dateFormat) {
        String defaultFormat = dateFormat.format(new Date());
        System.out.println("Date using default Locale " + Locale.getDefault() + " and default formatting style: " + defaultFormat); 
    }
}

Output:

Print date using default DateFormat instance, DateFormat.getInstance()
Date using default Locale en_IN and default formatting style: 8/7/15 9:36 AM
Print date using just the Date Instance, DateFormat.getDateInstance()
Date using default Locale en_IN and default formatting style: 8 Jul, 2015
Print date using just the time Instance, DateFormat.getTimeInstance()
Date using default Locale en_IN and default formatting style: 9:36:42 AM
Print date using just the dateTime Instance, DateFormat.getDateTimeInstance()
Date using default Locale en_IN and default formatting style: 8 Jul, 2015 9:36:42 AM

DateFormat instance using different styles

Now we will try date formatting using different styles, the short, medium, long and full. The locale is still the default.

DateFormatStyleExample:

package com.javarticles.date;

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

public class DateFormatStyleExample {
    public static void main(String[] args) {
        DateFormatStyle[] dateFormatStyles = {
                new DateFormatStyle("Short", DateFormat.SHORT),
                new DateFormatStyle("Medium", DateFormat.MEDIUM),
                new DateFormatStyle("Long", DateFormat.LONG),
                new DateFormatStyle("Full", DateFormat.FULL) };
        
        System.out.println("*************************************");
        
        for (DateFormatStyle dateFormatStyle : dateFormatStyles) {
            DateFormat dateFormat = DateFormat
                    .getDateInstance(dateFormatStyle.style);
            System.out
                    .println("Print date using DateFormat.getDateInstance(" + dateFormatStyle + ")");
            printDate(dateFormat);
        }
        
        System.out.println("*************************************");
        
        for (DateFormatStyle dateFormatStyle : dateFormatStyles) {
            DateFormat dateFormat = DateFormat
                    .getTimeInstance(dateFormatStyle.style);
            System.out
                    .println("Print date using DateFormat.getTimeInstance(" + dateFormatStyle + ")");
            printDate(dateFormat);
        }
        
        System.out.println("*************************************");

        for (DateFormatStyle dateFormatStyle : dateFormatStyles) {
            DateFormat dateFormat = DateFormat
                    .getTimeInstance(dateFormatStyle.style);
            System.out
                    .println("Print date using DateFormat.getDateTimeInstance(" + dateFormatStyle + ")");
            printDate(dateFormat);
        }
    }

    private static void printDate(DateFormat dateFormat) {
        String defaultFormat = dateFormat.format(new Date());
        System.out.println("Date using default Locale " + Locale.getDefault()
                + " and default formatting style: " + defaultFormat);
    }

    private static class DateFormatStyle {
        private String styleDesc;
        private int style;

        DateFormatStyle(String styleDesc, int style) {
            this.styleDesc = styleDesc;
            this.style = style;
        }

        public String toString() {
            return styleDesc + " style";
        }
    }
}

Output:

*************************************
Print date using DateFormat.getDateInstance(Short style)
Date using default Locale en_IN and default formatting style: 8/7/15
Print date using DateFormat.getDateInstance(Medium style)
Date using default Locale en_IN and default formatting style: 8 Jul, 2015
Print date using DateFormat.getDateInstance(Long style)
Date using default Locale en_IN and default formatting style: 8 July, 2015
Print date using DateFormat.getDateInstance(Full style)
Date using default Locale en_IN and default formatting style: Wednesday, 8 July, 2015
*************************************
Print date using DateFormat.getTimeInstance(Short style)
Date using default Locale en_IN and default formatting style: 9:55 AM
Print date using DateFormat.getTimeInstance(Medium style)
Date using default Locale en_IN and default formatting style: 9:55:30 AM
Print date using DateFormat.getTimeInstance(Long style)
Date using default Locale en_IN and default formatting style: 9:55:30 AM IST
Print date using DateFormat.getTimeInstance(Full style)
Date using default Locale en_IN and default formatting style: 9:55:30 AM IST
*************************************
Print date using DateFormat.getDateTimeInstance(Short style)
Date using default Locale en_IN and default formatting style: 9:55 AM
Print date using DateFormat.getDateTimeInstance(Medium style)
Date using default Locale en_IN and default formatting style: 9:55:30 AM
Print date using DateFormat.getDateTimeInstance(Long style)
Date using default Locale en_IN and default formatting style: 9:55:30 AM IST
Print date using DateFormat.getDateTimeInstance(Full style)
Date using default Locale en_IN and default formatting style: 9:55:30 AM IST

DateFormat instance using different locales

In this example, we will use the overloaded versions of the DateFormat instance methods, for example, DateFormat.getDateInstance(DateFormat.FULL, Locale.CANADA).
We will pass different locales and try formatting the date. We have kept the style same, using Full style here.

LocaleDateFormatExample:

package com.javarticles.date;

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

public class LocaleDateFormatExample {
    public static void main(String[] args) {
        Locale [] locales = { Locale.CANADA, Locale.FRANCE, Locale.GERMANY, Locale.ITALY, Locale.US, Locale.UK };
        for(Locale locale : locales) {
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale);
            System.out
                    .println("Print date using DateFormat.getDateInstance(DateFormat.FULL," + locale + ")");
            printDate(dateFormat, locale);
        }        
    }

    private static void printDate(DateFormat dateFormat, Locale locale) {
        String format = dateFormat.format(new Date());
        System.out.println("Date using Locale " + locale
                + " and full formatting style: " + format);
    }
}

Output:

Print date using DateFormat.getDateInstance(DateFormat.FULL,en_CA)
Date using Locale en_CA and full formatting style: Wednesday, July 8, 2015
Print date using DateFormat.getDateInstance(DateFormat.FULL,fr_FR)
Date using Locale fr_FR and full formatting style: mercredi 8 juillet 2015
Print date using DateFormat.getDateInstance(DateFormat.FULL,de_DE)
Date using Locale de_DE and full formatting style: Mittwoch, 8. Juli 2015
Print date using DateFormat.getDateInstance(DateFormat.FULL,it_IT)
Date using Locale it_IT and full formatting style: mercoledì 8 luglio 2015
Print date using DateFormat.getDateInstance(DateFormat.FULL,en_US)
Date using Locale en_US and full formatting style: Wednesday, July 8, 2015
Print date using DateFormat.getDateInstance(DateFormat.FULL,en_GB)
Date using Locale en_GB and full formatting style: Wednesday, 8 July 2015

DateFormat using different styles for a given Locale

In our last example, we will try formatting for different styles and for a locale which is not the default one whichever we are interested in. I have used here Locale.CANADA.

LocaleDateFormatStyleExample:

package com.javarticles.date;

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

public class LocaleDateFormatStyleExample {
    public static void main(String[] args) {
        DateFormatStyle[] dateFormatStyles = {
                new DateFormatStyle("Short", DateFormat.SHORT),
                new DateFormatStyle("Medium", DateFormat.MEDIUM),
                new DateFormatStyle("Long", DateFormat.LONG),
                new DateFormatStyle("Full", DateFormat.FULL) };

        Locale locale = Locale.CANADA;
        for (DateFormatStyle dateFormatStyle : dateFormatStyles) {
            DateFormat dateFormat = DateFormat.getDateInstance(
                    dateFormatStyle.style, locale);
            System.out.println("Print date using DateFormat.getDateInstance("
                    + dateFormatStyle + ", " + locale + ")->"
                    + dateFormat.format(new Date()));
        }
    }

    private static class DateFormatStyle {
        private String styleDesc;
        private int style;

        DateFormatStyle(String styleDesc, int style) {
            this.styleDesc = styleDesc;
            this.style = style;
        }

        public String toString() {
            return styleDesc + " style";
        }
    }
}

Output:

Print date using DateFormat.getDateInstance(Short style, en_CA)->08/07/15
Print date using DateFormat.getDateInstance(Medium style, en_CA)->8-Jul-2015
Print date using DateFormat.getDateInstance(Long style, en_CA)->July 8, 2015
Print date using DateFormat.getDateInstance(Full style, en_CA)->Wednesday, July 8, 2015

Download the source code

This was an example of date formatting using DateFormat instance for different locales and styles.

You can download the source code here: javaDataFormatExample.zip

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at [email protected]

Comments are closed.