Java Date Time Java Java API
java.time.Period
public int getYears()
This method returns the amount of years of this Period object.
package com.logicbig.example.period;import java.time.Period;public class GetYearsExample { public static void main(String... args) { Period p = Period.of(100, 10, 20); System.out.println(p); int y = p.getYears(); System.out.println(y); }}
P100Y10M20D100
package com.logicbig.example.period;import java.time.Period;public class GetYearsExample2 { public static void main(String... args) { Period p = Period.ofMonths(1000); System.out.println(p); int y = p.getYears(); System.out.println(y); System.out.println("Normalizing"); Period p2 = p.normalized(); System.out.println(p2); int y2 = p2.getYears(); System.out.println(y2); }}
P1000M0NormalizingP83Y4M83