How do I format a date into dd/MM/yyyy?

Formatting how data should be displayed on the screen is a common requirement when creating a program or application. Displaying information in a good and concise format can be an added values to the users of the application. In the following code snippet we will learn how to format a date into a certain display format.

For these purposes we can utilize the DateFormat and SimpleDateFormat classes from the java.text package. We can easily format a date in our program by creating an instance of SimpleDateFormat class and specify to format pattern. Calling the DateFormat.format(Date date) method will format a date into a date-time string.

You can see the details about date and time patters in the following link: Date and Time Patterns. Now, let’s see an example as shown in the code snippet below.

package org.kodejava.text;

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) {
        Date date = Calendar.getInstance().getTime();

        // Display a date in day, month, year format
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
        String today = formatter.format(date);
        System.out.println("Today : " + today);

        // Display date with day name in a short format
        formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
        today = formatter.format(date);
        System.out.println("Today : " + today);

        // Display date with a short day and month name
        formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
        today = formatter.format(date);
        System.out.println("Today : " + today);

        // Formatting date with full day and month name and show time up to
        // milliseconds with AM/PM
        formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
        today = formatter.format(date);
        System.out.println("Today : " + today);
    }
}

Let’s view what we got on the console:

Today : 24/09/2021
Today : Fri, 24/09/2021
Today : Fri, 24 Sep 2021
Today : Friday, 24 September 2021, 09:36:32.724 AM

9 Comments

    • Hi Hsu,

      You can use the parse() method of the java.text.SimpleDateFormat to convert string to date. See the following snippets:

      DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
      Date today = df.parse("2020-02-28");
      
      Reply
      • DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date today = df.parse("2020-02-28");
        

        But this script again give result in this format Fri Feb 28 00:00:00 UTC 2020

    • Hi Mahesh,

      The java.util.Date object doesn’t have any format on its own. If you want to display it in certain format you can use the java.text.SimpleDateFormat as the example in the above code snippet shows.

      Reply
    • Hi Anand,

      You can do it like this:

      DateFormat df1 = new SimpleDateFormat("dd/MM/yyyy");
      DateFormat df2 = new SimpleDateFormat("MM/dd/yyyy");
      
      Date date = df1.parse("19/09/1999");
      String newFormat = df2.format(date);
      
      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.