How to format Date in Java

In this tutorial we will show you how to format date in Java. To format a date the JDK provides a SimpleDateFormat class. You can either define your own custom date format strings or use the built in date formats. You can also provide a java.util.Locale when formatting a date.

Format Date

Here is a concrete example how to format a date in java using different date formats. the SimpleDateFormat#format(date) method is used to format a date into a String. First we use custom date format strings and afterwards we use the default date formats.

package com.memorynotfound.date;

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

public class FormatDate {

    public static void main(String... args) throws InterruptedException {

        Date date = new Date();

        System.out.println("Custom format date strings");
        DateFormat df = new SimpleDateFormat("dd/MM/yy");
        System.out.println("Today is (dd/MM/yy): " + df.format(date));

        df = new SimpleDateFormat("dd/MM/yyyy");
        System.out.println("Today is (dd/MM/yyyy): " + df.format(date));

        df = new SimpleDateFormat("yyyy/MM/dd");
        System.out.println("Today is (yyyy/MM/dd): " + df.format(date));

        df = new SimpleDateFormat("d MMM, yyyy");
        System.out.println("Today is (d MMM, yyyy): " + df.format(date));

        df = new SimpleDateFormat("d MMMM, yyyy");
        System.out.println("Today is (d MMMM, yyyy): " + df.format(date));

        df = new SimpleDateFormat("d MMMM, yyyy", Locale.CHINESE);
        System.out.println("Today is (d MMMM, yyyy, CHINESE): " + df.format(date));

        df = new SimpleDateFormat("d MMMM, yyyy", Locale.ITALIAN);
        System.out.println("Today is (d MMMM, yyyy, ITALIAN): " + df.format(date));


        System.out.println("\nBuilt in date formats");
        df = DateFormat.getDateInstance(DateFormat.DEFAULT);
        System.out.println("Today is (DateFormat.DEFAULT): " + df.format(date));

        df = DateFormat.getDateInstance(DateFormat.SHORT);
        System.out.println("Today is (DateFormat.SHORT): " + df.format(date));

        df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        System.out.println("Today is (DateFormat.MEDIUM): " + df.format(date));

        df = DateFormat.getDateInstance(DateFormat.LONG);
        System.out.println("Today is (DateFormat.LONG): " + df.format(date));

        df = DateFormat.getDateInstance(DateFormat.FULL);
        System.out.println("Today is (DateFormat.FULL): " + df.format(date));

        df = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINESE);
        System.out.println("Today is (DateFormat.FULL, CHINESE): " + df.format(date));

        df = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALIAN);
        System.out.println("Today is (DateFormat.FULL, ITALIAN): " + df.format(date));

    }
}

Generated output

The previous generates the following output

Custom format date strings
Today is (dd/MM/yy): 24/12/15
Today is (dd/MM/yyyy): 24/12/2015
Today is (yyyy/MM/dd): 2015/12/24
Today is (d MMM, yyyy): 24 Dec, 2015
Today is (d MMMM, yyyy): 24 December, 2015
Today is (d MMMM, yyyy, CHINESE): 24 十二月, 2015
Today is (d MMMM, yyyy, ITALIAN): 24 dicembre, 2015

Built in date formats
Today is (DateFormat.DEFAULT): Dec 24, 2015
Today is (DateFormat.SHORT): 12/24/15
Today is (DateFormat.MEDIUM): Dec 24, 2015
Today is (DateFormat.LONG): December 24, 2015
Today is (DateFormat.FULL): Thursday, December 24, 2015
Today is (DateFormat.FULL, CHINESE): 2015年12月24日 星期四
Today is (DateFormat.FULL, ITALIAN): giovedì 24 dicembre 2015

References

Download

Download it ! – format date java

You may also like...