Java Date Time - Period getYears() example








Period getYears() gets the amount of years of this period. This returns the years unit.

Syntax

getYears has the following syntax.

public int getYears()

Example

The following example shows how to use getYears.

import java.time.Period;
//  ww w . j a  v  a 2 s.co m
public class Main {
  public static void main(String[] args) {
    Period p = Period.ofMonths(1);
    
    System.out.println(p.getYears());

  }
}

The code above generates the following result.