Period getDays() method in Java

Java 8Object Oriented ProgrammingProgramming

The number of days for a particular Period can be obtained using the getDays() method in the Period class in Java. This method requires no parameters and it returns the number of days in the Period.

A program that demonstrates this is given as follows

Example

Live Demo

import java.time.Period;
public class Demo {
public static void main(String[] args) {
String period = "P5Y7M15D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The number of days are: " + p.getDays());
}
}



Output

The Period is: P5Y7M15D
The number of days are: 15

Now let us understand the above program.

First the Period is displayed. Then the number of days for the Period are obtained using the getDays() method and displayed. A code snippet that demonstrates this is as follows:

String period = "P5Y7M15D";
Period p = Period.parse(period);
System.out.println("The Period is: " + p);
System.out.println("The number of days are: " + p.getDays());
Updated on 30-Jul-2019 22:30:25