Java Get Yesterday Date

Tags: Java Date DateUtil

In this Java tutorial, we learn how to get yesterday date and time as a Date object in Java programming language.

How to get yesterday Date in Java

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

DateUtil.java

import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * This method to get yesterday Date object
     * @return the yesterday Date object
     */
    public static Date getYesterdayDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }
}

In the following example Java program, we show how to use the above DateUtil class to get yesterday Date object.

GetYesterdayExample.java

import java.util.Date;

public class GetYesterdayExample {
    public static void main(String... args) {
        // Get Yesterday Date
        Date yesterday = DateUtil.getYesterdayDate();

        System.out.println("Yesterday: " + yesterday);
    }
}
The output as below.
Yesterday: Tue Aug 16 20:23:59 ICT 2022

Happy Coding 😊

Java Get Tomorrow Date

Java Get Yesterday LocalDate

Java Get Tomorrow LocalDate

Java Get First Day of Month from Specified Date

Java Get Last Date of Current Month

Java Get Last Date of Current Year

Java Get Last Date of Specified Month

Java Get Last Date of Specified Year

Java Check if Calendar is Week Day or Weekend Day

Java Check if Date is Week Day or Weekend Day

Java Check if Today is Week Day or Weekend Day

Java Convert String to Date

Java Convert Date to Start of Day Time

Java Convert Date to End of Day Time

Java Get First Date of Current Year

Java Get First Date of Current Month

Java Get Same Date in Last Month