Java format LocalDate value to String

Tags: LocalDate LocalDate format DateTimeFormatter DateTimeFormatter ofPattern

In this Java core tutorial we learn how to format a LocalDate value to formatted String using the DateTimeFormatter class.

How to format date using DateTimeFormatter in Java

FormatLocalDateExample1.java

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample1 {
    public static void main(String... args) {
        LocalDate localDate = LocalDate.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        String formattedDate = localDate.format(dateTimeFormatter);

        System.out.println(formattedDate);
    }
}
The output is:
26-07-2021

FormatLocalDateExample2.java

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample2 {
    public static void main(String... args) {
        LocalDate localDate = LocalDate.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("E, MMM dd yyyy");
        String formattedDate = localDate.format(dateTimeFormatter);

        System.out.println(formattedDate);
    }
}
The output is:
Mon, Jul 26 2021

FormatLocalDateExample3.java

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class FormatLocalDateExample3 {
    public static void main(String... args) {
        LocalDate localDate = LocalDate.now();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yy");
        String formattedDate = localDate.format(dateTimeFormatter);

        System.out.println(formattedDate);
    }
}
The output is:
26/07/21

Happy Coding 😊