Java date format dd-mm-yyyy



In this source code example, we will you how to format a Date into DD-MM-YYYY format in Java.

In Java, the SimpleDateFormat class provides methods to format and parse date and time in java. 

So we use SimpleDateFormat class methods to format the Date.

Java Format date to dd-mm-yyyy

The below Java program demonstrates how to format Date into DD-MM-YYYY format:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    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);
    }
}

Output:

Today : 10-11-2021
Today : Wed, 10-11-2021
Today : Wed, 10 Nov 2021
Today : Wednesday, 10 November 2021, 04:27:58.875 pm

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments