Java – Add days to date
In this article, we will take a look at 2 different ways to add/subtract days, months or years from current date as well as any given date in java with examples.
Java 8 LocalDate has a
now()
method which returns an object having current date.To add days to this date, it has
plusDays()
method which accepts a long value representing the number of days to be added to this date. Example, // get current date LocalDate currentDate = LocalDate.now(); // add 30 days LocalDate plusDays = currentDate.plusDays(30); System.out.println("Current Date: " + currentDate); System.out.println("Date after 30 days: " + plusDays);
Output is
Current Date: 2022-07-04
Date after 30 days: 2022-08-03
LocalDate
also provides methods to add months, weeks and years with plusMonths()
, plusWeeks()
and plusYears()
methods respectively.
It also has corresponding methods to subtract these values from a date.
Remember that LocalDate
is an immutable class. This means that all these methods return a new object rather than modifying the original object.
2. LocalDate – Add days to another date
Create a date using LocalDate.of()
method.
of()
takes year, month and date as arguments and returns a date with these values.
Use plusDays()
, plusMonths()
, plusWeeks()
and plusYears()
methods to add values to this date as shown above. Example,
LocalDate currentDate = LocalDate.of(2022, 1,1); LocalDate plusDays = currentDate.plusMonths(2); System.out.println("Current Date: " + currentDate); System.out.println("Date after 2 months: " + plusDays);
Output is
Current Date: 2022-01-01
Date after 2 months: 2022-03-01
java.util.Calendar
object provides a generic add()
method which accepts 2 arguments:A. Date component that needs to be added or subtracted.
B. Number that will be added/subtracted.
To create a calendar object, use its static getInstance()
method and to get the date encapsulated by this object, use its getTime()
method.
Below code adds 7 days to current date.
// create calendar object Calendar calendar = Calendar.getInstance(); System.out.println("Current date: " + calendar.getTime()); // add 7 days calendar.add(Calendar.DATE, 7); System.out.println("Date after 7 days: " + calendar.getTime());
Output is
Current date: Mon Jul 04 23:08:08 IST 2022
Date after 7 days: Mon Jul 11 23:08:08 IST 2022
To subtract a value from current date, provide a negative value to the second argument to add()
.
Note that getTime()
internally creates a Date
object using its constructor.
4. Calendar – Add days to other date
To create a date other than current date, java.util.Calendar
object provides a set()
method.
Using set()
, we can set the value of a date component(such as day, month or year) to some other value.
Once the calendar is updated to a desired date, we can add days to it using add()
method. Example,
Calendar calendar = Calendar.getInstance(); // set calendar to other date calendar.set(Calendar.DATE, 31); calendar.set(Calendar.MONTH, 11); calendar.set(Calendar.YEAR, 2022); System.out.println("Current date: " + calendar.getTime()); // add 1 day calendar.add(Calendar.DATE, 1); System.out.println("Date after 1 day: " + calendar.getTime());
Output is
Current date: Sat Dec 31 23:18:47 IST 2022
Date after 1 day: Sun Jan 01 23:18:47 IST 2023
Note that calendar counts the months starting from 0. So, a month set to 11 means December.
A value greater than 12 will set the month accordingly without raising any error.