What is Period.ofDays() in Java?

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;

Syntax


public static Period ofDays(int days)

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

Free Resources