Java

How to Add One Day to a Date in Java

There is no predefined Date class in Java; however, you can get the date and time by importing the java.time and java.util packages in your project. The date and time are also provided by the java.util package. Numerous date and time classes are also part of the java.util package.

This post will show you how to add a day to a date in Java.

How to Add One Day to a Date in Java?

For adding one day to a date in Java, you can use methods of the below-mentioned classes:

    • Using LocalDate class
    • Using Instant class
    • Using Calendar class
    • Using Date class

Let’s look at each of them one by one!

Method 1: Add One Day to a Date Using LocalDate Class

To add one day to a date, you can use Java “LocalDateTime” Class with “now()” and “plusDays()” methods. The now() method is used to fetch the current date, and the plusDate() method is utilized for adding a specified number of days to LocalDate.

Syntax

To add a day to a date using the LocalDateTime class, use the syntax provided below:

todayDate.plusDays(1);

 
The “todayDate” is the current localDate class object that calls the “plusDays()” method to add a day to the local date by passing the number “1” as an argument.

Example

First, we will get the local date using the “now()” method of the LocalDateTime class and store it in a variable “todayDate”:

LocalDateTime todayDate = LocalDateTime.now();

 
Now, we will add one day to the date by passing “1” as an argument to the “plusDays()” method:

LocalDateTime tomorrowDate = todayDate.plusDays(1);

 
Print the today’s date that is stored in the variable “todayDate”:

System.out.println("Today's Date:"+todayDate);

 
Then, print the next day that is stored in the variable “tomorrowDate”:

System.out.println("Adding One Day:" + tomorrowDate );

 

The output shows today’s and tomorrow’s date using LocalDateTime class:


Let’s try another method for adding one day to a date in Java.

Method 2: Add One Day to a Date Using Instant Class

In this section, the “toInstant()” method of the “Instant” class will be utilized to obtain the current date and time. Then, add days to today using the “plus()” method. The Instant and Date classes of Java are almost similar.

Syntax

To use the plus() method of the Instant Class, follow the below-given syntax:

todayInstant.plus(1,ChronoUnit.DAYS);

 
Here, “todayInstant” is an object of the Instant class used to invoke the plus() method by passing “1” as an argument and “ChronoUnit.DAYS” is a unit representing the concept of a day.

Example

First, we will create an instance “date” of the “Date” class using the “new” keyword:

Date date = new Date();

 
Then, call the “toInstant()” method of the Instant class with the date object to get the today’s date:

Instant todayInstant = date.toInstant();

 
Invoke the “plus()” method to add a day in today’s date and store it in a variable “tomorrowDate”:

Instant tomorrowDate = todayInstant.plus(1,ChronoUnit.DAYS);

 
Finally, print the next day’s date on the console:

System.out.println("Adding One Day:" + tomorrowDate);

 

The output indicates that one day is added to today’s date successfully:


Now, let’s see how the Date class works for adding a day to a date in Java.

Method 3: Add One Day to a Date Using Date Class

The “Date” class is the most common class in Java used for accessing date and time. For getting the date and time of the next day, the “getTime()” method of the Date class is utilized. As the constructor of the Date class uses milliseconds so we will add the time of the next day in milliseconds.

Syntax

The below-given syntax is used for the Date class for the specified purpose:

new Date(date.getTime() + (1000 * 60 * 60 * 24));

 
The “(1000 * 60 * 60 * 24)” represents time in milliseconds like “24” hours, “60” minutes, “60” seconds and “1000” indicates the milliseconds.

Example

First, we will create an instance of the Date class, which automatically stores today’s date:

Date date = new Date();

 
Then, we will print today’s date and get the date and time of the next day using the “getTime()” method:

Date tomorrowDate = new Date(date.getTime() + (1000 * 60 * 60 * 24))

 
Lastly, print the date of the next day using the “System.out.println()” method:

System.out.println("Adding One Day:" + tomorrowDate);

 

Output


Let’s try another approach to add a day to a date in Java.

Method 4: Add One Day to a Date Using Calendar Class

There is another class for dates and times called the “Calendar” class. You can also utilize it to add a day to date.

For adding a day to date, first, we need to get today’s date using the “getInstance()” method of the Calendar class and set that date using the “setTime()” method. Then, for adding a day, utilize the “add()” method of the Calendar class.

Syntax

Follow the given syntax for adding one day to date using the “Calendar” class:

cal.add(Calendar.DATE, 1);

 
Here, “cal” in an instance of the Calendar class is used to invoke the add() method by passing “Calendar.DATE” to get today’s date and “1” for adding one day to it.

Example

We will first create a Date class object in this example:

Date date = new Date();

 
Then, we will create a Calendar class instance and set the date to the specified date using the “getInstance()” method:

Calendar cal = Calendar.getInstance();

 
Set the date using the Calendar class instance by invoking a “setTime()” method and passing a date object in it as an argument:

cal.setTime(date);

 
Then, add a day to the date by passing “1” as an argument to add it in the “Calender.DATE”:

cal.add(Calendar.DATE, 1);

 
In the date object, we will get the date and time of the next day using the “getTime()” method:

date = cal.getTime();

 
Finally, we will print the value of the date object by invoking the “System.out.println()” method:

System.out.println("Adding One Day:" + date);

 

Output indicates that we successfully added a day in a date using Calendar class:


We have provided all the necessary information related to adding one day to a date in Java.

Conclusion

For adding one day to date, Java provides classes having predefined methods including, LocalDate class, Instant class, Calendar class, and Date class. These classes are included in the java.time and java.util packages. All of these Java classes use different methods such as “plus()”, “plusDays()”, “add()”, and others to add one day to date. This post offered several ways to add one day to a date in Java with proper examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.