What is the Period.plusDays() method in Java?

plusDays() is an instance method of the Period class that is used to add the specified number of days. The year and month units are unaffected.

The plusDays() method is defined in the Period class. The Period class is defined in the java.time package. To import the Period class, we can use the following import statement:

import java.time.Period;

Syntax


public Period plusDays(long daysToAdd)

Parameters

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

Return value

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

Code

In the code below, we add three days to the basePeriod object with the help of the plusDays() method in line 17. We print the new object to the console with the command in line 18.

import java.time.Period;
public class Main{
public static void main(String[] args) {
int numYears = 5;
int numMonths = 10;
int numDays = 13;
// Define a period object
Period basePeriod = Period.of(numYears, numMonths, numDays);
// number of days to add
int daysToAdd = 3;
// New Period object after the addition
Period newPeriod = basePeriod.plusDays(daysToAdd);
System.out.printf("%s + %s days = %s", basePeriod, daysToAdd, newPeriod);
System.out.println();
}
}

Free Resources