Javatpoint Logo
Javatpoint Logo

Get Yesterday's Date Using Date Class Java

The Date class in Java provides a basic means of working with dates and timings. In this section, we'll examine how to use Java's Date class to retrieve the date from yesterday.

Before we begin, it's important to remember that Java 8 deprecated the Date class. The class's improper handling of time zones and other problems cause this. The new date and time API, which is far more capable and versatile than the previous Date class, is advised for usage with dates and times in Java 8 and subsequent versions.

For those still using older Java versions, we will look at how to obtain yesterday's date using the Date class.

Using the Date class, it is rather easy to obtain yesterday's date in Java. This may be done by taking today's date and removing one day. To do this, we must first construct a Date class object, which we can then use to update the date and time to the current system time using the setTime() function. Once we have done this, we can deduct a day from the date by creating a new instance of the Date class and subtracting the number of milliseconds in a day from the current date's time value.

The Date class must first be imported, so add the following line at the start of your Java file:

Next, we create an instance of the Date class and set its time to the current system time using the setTime() method:

In this line of code, we are utilizing the getTime() function to remove the number of milliseconds per day from the current date's time value (1000 * 60 * 60 * 24). The Date object's time value in milliseconds since January 1, 1970, UTC is returned via the getTime() function. This tells us how much time yesterday's date is worth.

When we know the time value for yesterday's date, we can use the Date function Object() to create a new instance of the Date class. This new instance will represent the previous date.

The SimpleDateFormat class is then used to format yesterday's date. Dates are formatted and parsed according to a certain format using the SimpleDateFormat class.

The format string "dd-MM-yyyy" is sent to a new instance of the SimpleDateFormat class we construct. The pattern in which the date should be shown is specified by the format string. The format string's "dd" character designates the month's day, the "MM" character designates the month, and the "yyyy" character designates the year.

Last, we format yesterday's date using the format() function of the SimpleDateFormat class. The date is returned as a String using the format() function in the given format.

YesterdayDateExample.java

Output:

Yesterday's date: 05-04-2023

Explanation

We import the Date and SimpleDateFormat classes first in the code above. The setTime() function is then used to create an instance of the Date class and set it to the current system time. Then, by deducting a day from the current date's time value, we construct a new instance of the Date class that represents yesterday's date. The SimpleDateFormat class is then used to format yesterday's date and print it to the console.

Conclusion

Using the Date class in Java to obtain yesterday's date entails creating an instance of the class, setting its time to the current system time using the setTime() method, creating a new instance of the class by deducting a day from the current date's time value, formatting the date using the SimpleDateFormat class, and printing it to the console. Although the Date class is no longer supported in Java 8 and newer, it is still helpful for individuals still using earlier Java versions.

Last, we format yesterday's date using the format() function of the SimpleDateFormat class. The date is returned as a String using the format() function in the format that was given.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA