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:
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”:
Now, we will add one day to the date by passing “1” as an argument to the “plusDays()” method:
Print the today’s date that is stored in the variable “todayDate”:
Then, print the next day that is stored in the variable “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:
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:
Then, call the “toInstant()” method of the Instant class with the date object to get the today’s date:
Invoke the “plus()” method to add a day in today’s date and store it in a variable “tomorrowDate”:
Finally, print the next day’s date on the console:
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:
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:
Then, we will print today’s date and get the date and time of the next day using the “getTime()” method:
Lastly, print the date of the next day using the “System.out.println()” method:
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:
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:
Then, we will create a Calendar class instance and set the date to the specified date using the “getInstance()” method:
Set the date using the Calendar class instance by invoking a “setTime()” method and passing a date object in it as an argument:
Then, add a day to the date by passing “1” as an argument to add it in the “Calender.DATE”:
In the date object, we will get the date and time of the next day using the “getTime()” method:
Finally, we will print the value of the date object by invoking the “System.out.println()” method:
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.