Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

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

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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();
}
}

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring