Menu

Wednesday, July 6, 2011

Find no of days between two dates in java

  • 01. import java.util.Calendar;
  • 02. 
  • 03. public class DaysBWDates {
  • 04.     public static void main (String[] args) {
  • 05.         Calendar cal1 = Calendar.getInstance();
  • 06.         Calendar cal2 = Calendar.getInstance();
  • 07.         cal2.add(Calendar.DATE, -3);
  • 08.
  • 09.       long noOfDays = (cal1.getTimeInMillis()
  •                             - cal2.getTimeInMillis())/(1000*60*60*24);
  • 10. 
  • 11.         System.out.println("Date1: " + cal1.getTime());
  • 12.         System.out.println("Date2: " + cal2.getTime());
  • 13.         System.out.println ("No of days between two dates: " + noOfDays);
  • 14.     }
  • 15. }