Java Data Type How to - Create time different in years and months








Question

We would like to know how to create time different in years and months.

Answer

import java.time.Period;
public class Main {
  public static void main(String[] argv) {
    int yearsAtOctanner = 4;
    int monthsAtOctanner = 6;
    Period periodAtOct = Period.ofYears(yearsAtOctanner).withMonths(monthsAtOctanner);
    System.out.println("periodAtOct = " + periodAtOct);
  }
}

The code above generates the following result.