Java Data Type How to - Get the difference between date in days








Question

We would like to know how to get the difference between date in days.

Answer

import java.util.Date;
import java.util.GregorianCalendar;
//from  w  w  w.  j a va  2 s.c  o m
public class Main {
  public static void main(String[] av) {
    Date d1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();

    Date today = new Date();

    long diff = today.getTime() - d1.getTime();

    System.out.println("The 21st century (up to " + today + ") is "
        + (diff / (1000 * 60 * 60 * 24)) + " days old.");
  }
}

The code above generates the following result.