How to Add Days to Date in Java

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.

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.

Output:

Adding 1 day to current date: 2020-12-23
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.

Output:

Adding 1 day to the given date: 2020-12-26
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.

Output:

Current date: Tue Dec 22 13:08:11 IST 2020
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.

Output:

Current date: Tue Dec 22 13:08:11 IST 2020
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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *