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;
public Duration plusDays(long daysToAdd)
long daysToAdd
: The number of days to add. It can be positive or negative.This method returns a new instance of the Duration
class with the number of days added.
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 objectDuration baseDuration = Duration.ofDays(3);// number of days to addint daysToAdd = 3;// New Duration object after the additionDuration newDuration = baseDuration.plusDays(daysToAdd);System.out.printf("%s + %s days = %s", baseDuration, daysToAdd, newDuration);System.out.println();}}