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.

Related Posts

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java Using toInstant() method of Date class You […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which we can from Zone. Here is […]

  • 30 December

    How to get day name from date in java

    Learn about how to get day name of week from date in java

  • 23 March

    Get current timestamp in java

    In this post, we will see how to get current timestamp in java. There are multiple ways to get current timestamp in java. Using Java 8’s Instant class There are three ways to get timestamp using Java 8‘s java.time.Instant class. Using Instant.now() [crayon-60082e191c959494993172/] Using date.toInstant() [crayon-60082e191c95c581334933/] Using timestamp.toInstant() [crayon-60082e191c95d658790209/] Here is complete example for get […]

  • 22 April

    How to calculate difference between two dates in java

    In this post, we will see how to calculate difference between two dates. Sometimes we have requirement to find no. of days between two dates or no. of hours between two dates. Java Program: [crayon-60082e191cbf8989349763/] When you run above program, you will get following output: [crayon-60082e191cbfd070206120/]

  • 25 January

    How to convert String to Date in java

    In this post, we will see how to convert String to Date object in java. It is used when we have formatted String and need to convert it to Date object.You may also check how to convert Date to String Java program: [crayon-60082e191ce3f359171246/] When you run above program, you will get following output: [crayon-60082e191ce44720521922/]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.