Java Data Type How to - Get Elapsed Years between two Local Date








Question

We would like to know how to get Elapsed Years between two Local Date.

Answer

import java.time.LocalDate;
import java.time.Period;
//from w  w w .j  a  v a  2s  .com
public class Main {

  public static void main(String[] args) {
    System.out.println(getElapsedYears(LocalDate.MAX, LocalDate.MIN));
  }

  private static int getElapsedYears(LocalDate targetDate, LocalDate today) {
    return Period.between(targetDate, today).getYears();
  }
}

The code above generates the following result.