What is Duration.plusDays() in Java?

plusDays() is an instance method of the Duration class which is used to add the specified duration in standard 24 hours to the Duration object. The number of days is multiplied by 86400 to obtain the number of seconds to add. This is based on the standard definition of a day, which is 24 hours.

The plusDays() method is defined in the Duration class. The Duration class is defined in the java.time package. To import the Duration class, check the following import statement.

import java.time.Duration;

Syntax


public Duration plusDays(long daysToAdd)

Parameters

  • long daysToAdd: The number of days to add. It can be positive or negative.

Return value

This method returns a new instance of the Duration class with the number of days added.

Code

In the code below, we add three days to the baseDuration object with the help of the plusDays() method and print the new object to the console.

import java.time.Duration;
public class Main{
public static void main(String[] args) {
// Define a duration object
Duration baseDuration = Duration.ofDays(3);
// number of days to add
int daysToAdd = 3;
// New Duration object after the addition
Duration newDuration = baseDuration.plusDays(daysToAdd);
System.out.printf("%s + %s days = %s", baseDuration, daysToAdd, newDuration);
System.out.println();
}
}

Free Resources