Java 8 LocalDate Format Examples

In this tutorial, we will see the following LocalDate format examples:

  1. Java format LocalDate to yyyy/MM/dd
  2. Java format LocalDate to dd/MM/yyyy
  3. Java format LocalDate to yyyy-MM-dd
  4. Java format LocalDate to dd-MM-yyyy

Example 1: Java format LocalDate to yyyy/MM/dd

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        System.out.println("Before JDK 8:");
        // yyyy-MM-dd
        Date date = new Date();
        
        SimpleDateFormat formatterD1 = new SimpleDateFormat("yyyy/MM/dd");
        String d1 = formatterD1.format(date);
        
        System.out.println(d1);
        
        // yyyy-MM-dd HH:mm:ss
        SimpleDateFormat formatterD2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String d2 = formatterD2.format(date);
        
        System.out.println(d2);
        
        // E MMM yyyy HH:mm:ss.SSSZ
        SimpleDateFormat formatterD3 = new SimpleDateFormat("E MMM yyyy HH:mm:ss.SSSZ");
        String d3 = formatterD3.format(date);
        
        System.out.println(d3);
        
        System.out.println("\nStarting with JDK 8:");
        
        // yyyy-MM-dd
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String ld1 = formatterLocalDate.format(localDate);

        // or shortly
        String ld2 = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

        System.out.println(ld1);
        System.out.println(ld2);
    }
}

Code snippet to format a LocalDate into YYYY/MM/DD format:


        // yyyy-MM-dd
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        String ld1 = formatterLocalDate.format(localDate);

Output:

Before JDK 8:
2021/11/10
2021/11/10 16:38:20
Wed Nov 2021 16:38:20.048+0530

Starting with JDK 8:
2021/11/10
2021/11/10

Example2: Java format LocalDate to yyyy-MM-dd


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        System.out.println("Before JDK 8:");
        // yyyy-MM-dd
        Date date = new Date();
        
        SimpleDateFormat formatterD1 = new SimpleDateFormat("yyyy-MM-dd");
        String d1 = formatterD1.format(date);
        
        System.out.println(d1);
        
        // yyyy-MM-dd HH:mm:ss
        SimpleDateFormat formatterD2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d2 = formatterD2.format(date);
        
        System.out.println(d2);
        
        // E MMM yyyy HH:mm:ss.SSSZ
        SimpleDateFormat formatterD3 = new SimpleDateFormat("E MMM yyyy HH:mm:ss.SSSZ");
        String d3 = formatterD3.format(date);
        
        System.out.println(d3);
        
        System.out.println("\nStarting with JDK 8:");
        
        // yyyy-MM-dd
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String ld1 = formatterLocalDate.format(localDate);

        // or shortly
        String ld2 = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        System.out.println(ld1);
        System.out.println(ld2);
    }
}

Code snippet to format a LocalDate into YYYY-MM-DD format:


        // yyyy-MM-dd
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String ld1 = formatterLocalDate.format(localDate);


Output:

Before JDK 8:
2021-11-10
2021-11-10 16:38:20
Wed Nov 2021 16:38:20.048+0530

Starting with JDK 8:
2021-11-10
2021-11-10

Example 3: Java format LocalDate to dd-MM-yyyy


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        System.out.println("Before JDK 8:");
        // yyyy-MM-dd
        Date date = new Date();
        
        SimpleDateFormat formatterD1 = new SimpleDateFormat("dd-MM-yyyy");
        String d1 = formatterD1.format(date);
        System.out.println(d1);
        
        System.out.println("\nStarting with JDK 8:");
        
        // yyyy-MM-dd
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        String ld1 = formatterLocalDate.format(localDate);
        System.out.println(ld1);
        
        // or shortly
        String ld2 = LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy"));
        System.out.println(ld2);
    }
}

Output:

Before JDK 8:
10-11-2021

Starting with JDK 8:
10-11-2021
10-11-2021

Example 4: Java format LocalDate to dd/MM/yyyy


import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Main {

    public static void main(String[] args) {

        System.out.println("Before JDK 8:");
        // yyyy-MM-dd
        Date date = new Date();
        
        SimpleDateFormat formatterD1 = new SimpleDateFormat("dd/MM/yyyy");
        String d1 = formatterD1.format(date);
        System.out.println(d1);
        
        System.out.println("\nStarting with JDK 8:");
        
        // yyyy-MM-dd
        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatterLocalDate = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        String ld1 = formatterLocalDate.format(localDate);
        System.out.println(ld1);
        
        // or shortly
        String ld2 = LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
        System.out.println(ld2);
    }
}

Output:

Before JDK 8:
10/11/2021

Starting with JDK 8:
10/11/2021
10/11/2021

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


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

Comments