Javatpoint Logo
Javatpoint Logo

Get Yesterday's Date by No of Days in Java

Obtaining the date for a certain number of days prior to the current date is frequently required in Java. By utilizing the Java Date and Calendar classes, this may be accomplished. In this post, we will go through how to retrieve yesterday's date in Java by a given number of days.

In Java, dates and times are represented using the java. util.Date class, which represents a specific instant in time with millisecond precision. To perform date arithmetic in Java, we use java.util.Calendar class provides a set of methods for working with dates and times.

Java requires that we deduct a specific number of days from the current date in order to obtain yesterday's date. We can do this using the Calendar class's add() method, which allows us to add or subtract a certain number of units (days, months, etc.) from a date. In this case, we want to subtract a certain number of days, so we use the Calendar.DAY_OF_YEAR field to specify that we want to subtract days from the current year.

Using Java text, we may format the desired date once we obtain it. With the help of the SimpleDateFormat class, we can define a pattern for formatting dates. The date may then be formatted using the provided pattern by using the format() function.

Pseudo Code

  1. Import the required classes: java.util.Date, java.util.Calendar, and java.text.SimpleDateFormat.
  2. Set the number of days to go back in time by creating an integer variable and assigning the desired value to it.
  3. Create a Calendar object using the Calendar.getInstance() method.
  4. Using the calendar, deduct the given number of days from the current date. Method add().
  5. Use the calendar to retrieve yesterday's date as a Date object.
  6. the function getTime().Format yesterday's date using the SimpleDateFormat class and the desired pattern.
  7. Print yesterday's date to the console using the System.out.println() method.

Step 1

Import Java Date and Calendar classes as the first step.

Importing the necessary Java Date and Calendar classes is the first step. These classes are contained in Java. util package and are a part of the Java standard library.

The java.util.Date class represents a specific instant in time, with millisecond precision. The java.util.Calendar class provides a set of methods for working with dates and times, including adding or subtracting days from a date. The java.text.SimpleDateFormat class is used to format dates according to a specified pattern.

Step 2

Build a Calendar object in step two, then change the date to yesterday.

Create a Calendar object and set its date to yesterday as the next step. We may do this by taking the necessary number of days out of the current date.

In the code above, we first use the Calendar to build a Calendar object. the function getInstance(). This function results from a Calendar object with the current date and time in the default time zone. The calendar. add() function is then used to subtract the requested days from the current date. By dialing the calendar, we may finally obtain the date from yesterday. the function getTime().

Step 3

Format the date as necessary in step three.

We may format yesterday's date using the SimpleDateFormat class after we get it as a Date object. For instance, we can use the following code to format the date as "dd/MM/yyyy":

The code above generates a SimpleDateFormat object with the desired date format and uses the sdf.format() function to format the date from yesterday.

YesterdayDate.java

Output:

Yesterday's date: 02/04/2023






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