The plusDays() method of Period class in Java is used to add the days to this period. This method operates only on DAYS and does not affect other two YEAR, MONTH.
Syntax:
public Period plusDays(long daysToAdd)
Parameters: This method accepts a single parameter daysToAdd which is the number of days to be added from the period.
Return Value: It returns a Period based on provided period in the input adding the specified number of days. It must not be null.
Exceptions: It throws a ArithmeticException if numeric overflow occurs.
Below programs illustrate the above method:
Program 1:
// Java code to show the function plusDays() // to subtract the number of days from given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to subtract two given periods static void addDays(Period p1, int daystoAdd) { System.out.println(p1.plusDays(daystoAdd)); } // Driver Code public static void main(String[] args) { // Defining first period int year = 4; int months = 11; int days = 10; Period p1 = Period.of(year, months, days); int daystoAdd = 8; addDays(p1, daystoAdd); } } |
P4Y11M18D
Program 2: Period can be negative.
// Java code to show the function plusDays() // to subtract the number of days from given periods import java.time.Period; import java.time.temporal.ChronoUnit; public class PeriodClass { // Function to subtract two given periods static void addDays(Period p1, int daystoAdd) { System.out.println(p1.plusDays(daystoAdd)); } // Driver Code public static void main(String[] args) { // Defining first period int year = -4; int months = -11; int days = 0; Period p1 = Period.of(year, months, days); int daystoAdd = 8; addDays(p1, daystoAdd); } } |
P-4Y-11M8D
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#plusDays-long-
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- LocalDate plusDays() Method in Java with Examples
- Duration plusDays(long) method in Java with Examples
- LocalDateTime plusDays() method in Java with Examples
- ZonedDateTime plusDays() method in Java with Examples
- OffsetDateTime plusDays() method in Java with examples
- OffsetDateTime plusDays() method in Java with examples
- Period parse() method in Java with Examples
- Period getMonths() method in Java with Examples
- Period plusMonths() method in Java with Examples
- Period getDays() method in Java with Examples
- Period get() method in Java with Examples
- Period from() method in Java with Examples
- Period equals() method in Java with Examples
- Period between() method in Java with Examples
- Period negated() method in Java with Examples
- Period normalized() method in Java with Examples
- Period plusYears() method in Java with Examples
- Period isNegative() method in Java with Examples
- Period hashCode() method in Java with Examples
- Period getUnits() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

