Get the Current Date using LocalDate in Java


You can get the current date using LocalDate in Java by using the static now() method of the LocalDate class.


Let's see a few examples:

import java.time.LocalDate;

public class DateExample {

    public static void main(String... args) {

        LocalDate currentDate = LocalDate.now();
        System.out.println("The Current Date Is: " + currentDate);

    }
}

The Current Date Is: 2023-04-21

We have made use of the LocalDate class from the java.time package, which was introduced in Java 8 as a part of the new Date Time API.

Note: The now() method returns the current date as a LocalDate object based on the system clock in the default time zone.

If you want to know the date of a specific city, you will need to make use of the ZoneId class.


    Example:

    import java.time.LocalDate;
    import java.time.ZoneId;
    
    LocalDate currentDateInNewYork = LocalDate.now(ZoneId.of("America/New_York"));
    LocalDate currentDateInLondon = LocalDate.now(ZoneId.of("Europe/London"));
    
    System.out.println("Current Date in New York: " + currentDateInNewYork);
    System.out.println("Current Date in London: " + currentDateInNewYork);
    

    Current Date in New York: 2023-04-20
    Current Date in New York: 2023-04-21

    Get Current Date using LocalDate Class

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