Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is Period.ofDays() 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.

ofDays() is a static method of the Period class which is used to get an instance of the Period class representing the number of days. The number of days in the resultant period will be as specified. The value for years and months will be zero.

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

import java.time.Period;
Importing the period class

Syntax


public static Period ofDays(int days)
Syntax of the ofDays() method

Parameters

  • int days: The number of days. It can be positive or negative.

Return value

This method returns an instance of the Period class.

Code

In the code below, we create two Period class objects with a positive and negative number of days. Then we print the objects to the console.

import java.time.Period;
public class Main{
public static void main(String[] args) {
int numDays = 13;
Period period = Period.ofDays(numDays);
System.out.println("Positive number of days - " + period);
numDays = -13;
period = Period.ofDays(numDays);
System.out.println("Negative number of days - " + period);
}
}
Examples of Period.ofDays()

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