Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Period getYears() method in Java
The number of years for a particular Period can be obtained using the getYears() method in the Period class in Java. This method requires no parameters and it returns the number of years in the Period.
A program that demonstrates this is given as follows
Example
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 years are: " + p.getYears());
}
}
Output
The Period is: P5Y7M15D The number of years are: 5
Now let us understand the above program.
First the Period is displayed. Then the number of years for the Period are obtained using the getYears() 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 years are: " + p.getYears()); Advertisements
