Compute days between 2 dates


import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main{
  public static void main(String args[]) {
    Calendar c1 = new GregorianCalendar();
    Calendar c2 = new GregorianCalendar();
    c1.set(2000, 12, 12, 0, 0, 0);
    c2.set(2001, 12, 12, 0, 0, 0);

    System.out.println(daysBetween(c1.getTime(), c2.getTime()) + " day(s) between " + args[0] + "-"
        + args[1] + "-" + args[2] + " and " + args[3] + "-" + args[4] + "-" + args[5]);
  }

  static final long ONE_HOUR = 60 * 60 * 1000L;

  public static long daysBetween(Date d1, Date d2) {
    return ((d2.getTime() - d1.getTime() + ONE_HOUR) / (ONE_HOUR * 24));
  }
}
Home 
  Java Book 
    Runnable examples  

Date Calendar Timestamp:
  1. Create a Date for a Particular Date
  2. Create Date from specific time span
  3. Create new date without time component
  4. Create new java.sql.Timestamp
  5. Create Date with day, month and year
  6. Create Calendar for a specific date
  7. Compute days between 2 dates
  8. Calculate the age
  9. Time Distance in hours and minutes