Java Data Type How to - Get hiring length








Question

We would like to know how to get hiring length.

Answer

import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
/*from www . j  av a2 s .co  m*/
public class Main {

  public static void main(String[] args) {
    Period employmentPeriod = period(LocalDate.of(2000,
        Month.FEBRUARY, 1));
    int years = employmentPeriod.getYears();
    int months = employmentPeriod.getMonths(); 
    int days = employmentPeriod.getDays(); 

    System.out.println(years);
    System.out.println(months);
    System.out.println(days);
  }

  public static Period period(LocalDate hiringDate) {
    LocalDate today = LocalDate.now();
    return Period.between(hiringDate, today);
  }

}

The code above generates the following result.