Java Data Type How to - Count number of days difference between two dates








Question

We would like to know how to count number of days difference between two dates.

Answer

import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
/*from   w w w.j  ava  2  s  .c  om*/
public class Main {
  public static void main(String[] args) {

    LocalDate startEmployment = LocalDate.of(2011, Month.FEBRUARY, 1);

    LocalDate today = LocalDate.now();

    long numberOfDays = startEmployment.until(today, ChronoUnit.DAYS);

    System.out.println(String.format("%d", numberOfDays));


  }
}

The code above generates the following result.