Java Date add Days
In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between a particular point in time and a collection of calendar fields like DAY, MONTH, YEAR, HOUR, etc. The Similar interface is implemented by the Calendar class, which derives from the Object class.
How to declare a Calendar class in Java
Below is the syntax for declaring the calendar class:
abstract class Calendar extends Object implements Serializable, Cloneable, Comparable< Calendar>
With Java's Calendar class, we may add a single day or many days to the current Date or a supplied date. Let's examine the process for increasing the Date by a day.
How to add a given number of days to Date using calendar class
The steps below are used to operate the Calendar class to add days to the specified Date:
- We should first load all required courses and modules, including SimpleDateFormat and Calendar.
- Make a SimpleDateFormat and Calendar object. We interpret the data we want to add days using the objects created.
- Use the setTime() function of the calendar class to add the specified Date to the Calendar.
- We use the calendar classes add () method to add days to the Date. The calendar field and the amount of time that has to be added are the two parameters for the add method ().
- Take the obtained numerical Date from the Calendar and change the SimpleDateFormat class's default format to display it on the screen.
AddDaysExample1.java
// Package import section, all the required packages are imported
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class AddDaysExample1{
// Main method starts here
public static void main(String args[]){
//initializing the old Date as a string
String old = "2020-01-01";
System.out.println(old+" the data to which we have to add the days");
// creating the data format using the below method
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// Creating the object to the calendar class to use the methods of it
Calendar obj = Calendar.getInstance();
try{
obj.setTime(format.parse(old));
}
catch(ParseException e){
e.printStackTrace();
}
// using add() method of calendar class to add the days to the date
obj.add(Calendar.DAY_OF_MONTH, 5);
String dateAfter = format.format(obj.getTime());
//Date after adding five days to the old Date
System.out.println(dateAfter+" current date after adding 5 days to old date");
}
}
OUTPUT
2020-01-01 the data to which we have to add the days
2020-01-06 current Date after adding 5 days to old Date
Using calendar class, counting the days to the current Date
The procedures for adding days to the present Date and a specified date are identical. In this instance, defining a date is not necessary. Here, we take the Date from the Calendar and add days to it afterwards.
Let's look at a specific scenario to comprehend further how we might extend the current Date by days.
AddDaysExample2.java
// Package import section, all the required packages are imported
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.ParseException;
class AddDaysExample2{
// Main method starts here
public static void main (String args[]){
// creating the data format using the below method
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// Creating the object to the calendar class to use the methods of it
Calendar obj = Calendar.getInstance();
System.out.println(format.format(obj.getTime())+" is the date before adding days");
// using add method of calendar class to add the days to the date
obj.add(Calendar.DAY_OF_MONTH, -3);
String dateAfter = format.format(obj.getTime());
//Date after adding 5 days to the old Date
System.out.println(dateAfter+" is the date after adding 3 days.");
}
}
OUTPUT
2020-01-01 the data to which we have to add the days
2020-01-06 current Date after adding 5 days to old Date
Without using a calendar class, adding days to a date
Including days in the Date is relatively simple without utilizing the Calendar class. We make use of Java. Time package's LocalDate class. The LocalDate class offers several ways to determine and alter the current Date.
Let's look at an illustration to understand better how the LocalDate class in Java can be used to compute dates.
AddDaysExample3.java
// Package import section, all the required packages are imported
import java. time.LocalDate;
class AddDaysExample3{
// Main method starts here
public static void main (String args[]){
// Loading the current and adding one day to the current Date.
LocalDate date1 = LocalDate.now().plusDays(1);
System.out.println(date1+" is a day after adding 1 day to the current date");
// Loading the current Date and adding five days to the current Date.
LocalDate date2 = LocalDate.now().plusDays(5);
System.out.println(date2+" is a day after adding 7 days to the current date");
// Specifying a particular date adding one day to it
LocalDate date3 = LocalDate.of(2019, 10, 14). plusDays(1);
System.out.println(date3+" is a day after adding 1 day to the define date");
// Specifying a particular date adding seven days to it
LocalDate date4 = LocalDate.of(2019, 10, 14). plusDays(7);
System.out.println(date4+" is a day after adding 7 days to the define date");
}
}
OUTPUT
2022-09-11 is a day after adding 1 day to the current Date
2022-09-15 is a day after adding 7 days to the current Date
2019-10-15 is a day after adding 1 day to the define date
2019-10-21 is a day after adding 7 days to the define date