Javatpoint Logo
Javatpoint Logo

Get Yesterday's Date in Milliseconds Java

There are numerous techniques in Java to obtain the millisecond value of yesterday's date.

Method 1: Using java.util.Calendar

The Java.util.Date class and the Java.util.The Java legacy date and time API includes calendar classes. Although these classes are still accessible in Java, the more recent java.time package has mostly taken its place, and they are not advised for use in new work.

Here's an illustration showing how to use these classes to obtain yesterday's date in milliseconds:

YesterdayData1.java

Output:

Yesterday's date in milliseconds: 1680776986777

Explanation

We import the java.util.Calendar and java.util.Date classes first in this code.

The Date() constructor, which generates a new Date object according to the current date and time, is first used inside the main function to obtain the current date and time.

The next step is to construct a new Calendar object and use the setTime() function to set it to the current date. We remove one day from the calendar using the add() function and the DAY_OF_YEAR field with a value of -1. This changes the date on the calendar to yesterday.

Next, we use the getTime() function to transform the calendar into a Date object, which we add to the yesterday property.

Method 2: Using java.time.LocalDate and java.time.ZoneOffset Classes

The traditional date and time API was replaced with Java. Time package in Java 8. While the ZoneOffset class represents a defined time zone offset from UTC, the LocalDate class represents a date (year, month, and day) without a time or time zone.

Here's an illustration showing how to use these classes to obtain yesterday's date in milliseconds:

YesterdayDate2.java

Output:

Yesterday's date in milliseconds: 1680739200000

Method 3: Using java.time.LocalDateTime and java.time.ZoneOffset classes

A Java.time.The ZoneOffset class represents a fixed time zone offset from UTC, whereas the LocalDateTime class represents a date and time without a time zone. This approach is comparable to the first one, except it substitutes LocalDateTime for LocalDate.

Here's an illustration showing how to use these classes to obtain yesterday's date in milliseconds:

YesterdayDate3.java

Output:

Yesterday's date in milliseconds: 1680777396113

Explanation

We import Java.time first in this code.Java.time and LocalDateTime.Courses for ZoneOffset. Next, we developed a brand-new class named Main.

The first thing we do in the main function is use the LocalDateTime.now() method and the UTC zone offset to obtain the current date and time in UTC.

The minusDays() function is then used to obtain yesterday's date and time by deducting one day from the current time and date.

We then call the toEpochMilli() function to obtain the milliseconds since the epoch after converting yesterday's date and time to an Instant object using the toInstant() method and the UTC zone offset.

Finally, we use System to output yesterday's date in milliseconds.out.println().

Method 4: Using java.util.Calendar and java.util.TimeZone classes

Like Method 1, we use the time zone specified by the TimeZone class.

Here's an illustration showing how to use these classes to obtain yesterday's date in milliseconds:

YesterdayDate4.java

Conclusion

In this section, we have discussed various Java techniques for retrieving the data from yesterday in milliseconds. The ancient java.util.Date and java.util.Calendar classes, the more recent Java.time package, and the java.util.TimeZone classes are just a few of the classes and libraries used by these methods.







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