In this post, we will see how to add days to date in java.
There are multiple ways to add days to date in java. Let’s go through them.
Table of Contents
Using plusdays() method of LocalDate (Java 8)
If you want to add days to date without Calendar class, this is recommended way to add days to Date in java. It will work for Java 8 or later.
Let’s see with the help of example:
Add days to current date using LocalDate
We can use LocalDate.now() to get current date and use plusDays()
method to add days to LocalDate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package org.arpit.java2blog; import java.time.LocalDate; public class AddDaysToLocalDateMain { public static void main(String[] args) { //Add one Day to the current date LocalDate currentdDate1 = LocalDate.now(); LocalDate currentDatePlus1 = currentdDate1.plusDays(1); System.out.println("Adding 1 day to current date: "+currentDatePlus1); //Add number of Days to the current date LocalDate currentdDate7 = LocalDate.now(); LocalDate currentDatePlus7 = currentdDate7.plusDays(1); System.out.println("Adding 7 days to the current date: "+currentDatePlus7); } } |
Output:
Adding 7 days to the current date: 2020-12-23
Add days to given date using LocalDate
We can use LocalDate.now() to get current date and use plusDays()
method to add days to LocalDate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.time.LocalDate; public class AddDaysToLocalDateMain { public static void main(String[] args) { //Add one Day to the given date LocalDate datePlus1 = LocalDate.of(2020, 12, 25).plusDays(1); System.out.println("Adding 1 day to the given date: "+datePlus1); //Add number of Days to the given date LocalDate datePlus4 = LocalDate.of(2020, 12, 25).plusDays(4); System.out.println("Adding 7 days to the given date: "+datePlus4); } } |
Output:
Adding 7 days to the given date: 2020-12-29
Using add() method Calendar class
You can use add() method of Calendar class to add days to date, but this is not recommended in case you are using java 8 or later version.
Let’s see with the help of example:
Add days to current date using Calendar
We can use Calendar's setTime()
method to set current date and use add()
method to add days to LocalDate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package org.arpit.java2blog; import java.util.Calendar; import java.util.Date; public class AddDaysToCalendarMain { public static void main(String[] args) { Date currentDate = new Date(); System.out.println("Current date: "+currentDate); Calendar cal = Calendar.getInstance(); cal.setTime(currentDate); // add 1 days to current day cal.add(Calendar.DAY_OF_MONTH, 1); Date datePlus1 = cal.getTime(); System.out.println("Adding 1 days to current date: "+datePlus1); Calendar cal7 = Calendar.getInstance(); cal7.setTime(currentDate); // add 7 days to current day cal7.add(Calendar.DAY_OF_MONTH, 7); Date datePlus7 = cal7.getTime(); System.out.println("Adding 7 days to current date: "+datePlus7); } } |
Output:
Adding 1 days to current date: Wed Dec 23 13:08:11 IST 2020
Adding 7 days to current date: Tue Dec 29 13:08:11 IST 2020
Add days to given date using Calendar
We can use Calendar's setTime()
method to set current date and use add()
method to add days to LocalDate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; import java.util.Calendar; import java.util.Date; public class AddDaysToCalendarMain { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 10); cal.set(Calendar.MONTH, 0); cal.set(Calendar.YEAR, 2018); Date givenDate = cal.getTime(); System.out.println("Given date: "+givenDate); // add 1 days to current day cal.add(Calendar.DAY_OF_MONTH, 1); Date datePlus1 = cal.getTime(); System.out.println("Adding 1 days to current date: "+datePlus1); } } |
Output:
Given date: Wed Jan 10 13:14:18 IST 2018
Adding 1 days to current date: Thu Jan 11 13:14:18 IST 2018
That’s all about how to add days to date in java.