Pages

Loading...

Thursday, 27 October 2011

Java-Compare Dates

import java.util.Calendar;

public class CompareDates{ 
  public static void main(String[] args){
   Calendar calendar1 = Calendar.getInstance();
   Calendar calendar2 = Calendar.getInstance();
   calendar2.set(2011, 9, 19); // year,month,date
   calendar1.set(2011, 10, 12);// year,month,date
   long milliseconds1 = calendar1.getTimeInMillis();
   long milliseconds2 = calendar2.getTimeInMillis();
   long diff = milliseconds2 - milliseconds1;
   long diffDays = diff / (24 * 60 * 60 * 1000);
   System.out.println("Time in days: " + diffDays  + " days.");
  }
}