How to format LocalDate in Java using DateTimeFormatter


Formatting Java LocalDate
package org.code2care.java.examples;

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

public class LocalDateFormattingExample {

    public static void main(String[] args) {

        String dateFormat = "dd/MM/yyyy";
        LocalDate localDateNow = LocalDate.now(); //get the date
        System.out.println("LocalDate: " + localDateNow);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        String formattedDate = localDateNow.format(formatter);
        System.out.println("Formatted LocalDate: " + formattedDate);

    }
}

Output:

LocalDate: 2023-04-20
Formatted LocalDate: 20/04/2023



You can choose from the date formats based on the how it is represented in a specific country or region.

CountryDate FormatExample
United StatesMM/dd/yyyy09/28/2022
United Kingdomdd/MM/yyyy28/09/2022
Canadayyyy-MM-dd2022-09-28
Australiadd/MM/yyyy28/09/2022
Japanyyyy年MM月dd日2022年09月28日
Germanydd.MM.yyyy28.09.2022
Francedd/MM/yyyy28/09/2022
Indiadd/MM/yyyy28/09/2022
Chinayyyy年MM月dd日2022年09月28日
Brazildd/MM/yyyy28/09/2022


RegionDate FormatExample
North AmericaMM/dd/yyyy04/22/2023
Europedd/MM/yyyy22/04/2023
China, Japan, Koreayyyy/MM/dd2023/04/22
Middle Eastdd/MM/yyyy22/04/2023
India, Bangladesh, Nepaldd/MM/yyyy22/04/2023
Southeast Asiadd/MM/yyyy or yyyy-MM-dd22/04/2023 or 2023-04-22
Australia, New Zealanddd/MM/yyyy22/04/2023
Latin Americadd/MM/yyyy or MM/dd/yyyy22/04/2023 or 04/22/2023

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap