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

Overview

The getDays() method is an instance method of the Period class that gets the number of days of the Period instance.

Importing the Period class

The Period class is in the java.time package, and we can import it using the following import statement.

import java.time.Period;

Syntax


public int getDays()

Parameters

This method has no parameters.

Return value

This method returns the number of days of the Period object.

Code

In the below code, we retrieve the number of the days of the Period objects using the getDays() method and print the returned value to the console.

import java.time.Period;
public class Main{
public static void main(String[] args) {
// Define the different attributes of Period object
int numYears = 5;
int numMonths = 10;
int numDays = 13;
// Construct the Period object using the of() method
Period period = Period.of(numYears, numMonths, numDays);
// Get the number of days from the Period object using getDays() method
System.out.println("The number of days of the period " + period + " is " + period.getDays());
// Define the different attributes of Period object
numYears = -5;
numMonths = -10;
numDays = -13;
// Construct the Period object using the of() method
period = Period.of(numYears, numMonths, numDays);
// Get the number of days from the Period object using getDays() method
System.out.println("The number of days of the period " + period + " is " + period.getDays());
}
}

Free Resources