Java Get Yesterday LocalDate

Tags: Java LocalDate DateUtil

In this Java tutorial, we learn how to get the yesterday date and return it as a LocalDate object in Java programming language.

How to get yesterday LocalDate in Java

In this step, we create a new Java class named DateUtil, and implement a static method named getYesterdayLocalDate() to return yesterday LocalDate object as the following Java code.

DateUtil.java

import java.time.LocalDate;

public class DateUtil {

    /**
     * This method to get yesterday date and return it as a LocalDate object.
     * @return the yesterday LocalDate object.
     */
    public static LocalDate getYesterdayLocalDate() {
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        return  yesterday;
    }

}

The following example Java code, we learn how to use the above DateUtil class to get yesterday as a LocalDate object in Java program.

GetYesterdayLocalDateExample.java

import java.time.LocalDate;

public class GetYesterdayLocalDateExample {
    public static void main(String... args) {
        // Get Yesterday LocalDate
        LocalDate yesterday = DateUtil.getYesterdayLocalDate();

        System.out.println("Today: " + LocalDate.now());
        System.out.println("Yesterday: " + yesterday);
    }
}
The output as below.
Today: 2022-08-20
Yesterday: 2022-08-19

Happy Coding 😊

Java Get Tomorrow LocalDate

Java Get Yesterday Date

Java Get Tomorrow Date