Find difference between two dates in java with example.

How  to calculate no of days between two dates in Java:
       Difference between two dates can be calculated in many ways . I have calculated in two methods. One is using Java Util.Date and another is using Calendar class.
Method 1 :
1) Input two dates (dd/mm/yyyy) as a string using keyboard.
2) Convert the Dates String to Date format using DateFormat Class.
3) Calculate difference of two dates using Date getTime() method which returns the number of milliseconds since January 1,1970,00:00:00 GMT represented by this Date object
import java.io.*;
import java.util.*;
import java.text.*;

public class DateDiff  {
      public static void main(String [] args){
      try{
      BufferedReader br = new BufferedReader(
                  new InputStreamReader(System.in));

      DateFormat df = new SimpleDateFormat (“dd/MM/yyyy”);

      System.out.println(“Enter Date1”);

      String dateSt1= br.readLine();       // Accepts date1 as string

      System.out.println(“Enter Date2”); 

      String dateSt2= br.readLine();  // Accepts date2 as string

 Date date1=df.parse(dateSt1);     // Date String to Date
 Date date2=df.parse(dateSt2);
 
System.out.println(“Date1 = ” + dateSt1);
System.out.println(“Date2 = ” + dateSt2);

System.out.println(“Days between Date1 and Date2 = ” + (date2.getTime() – date1.getTime())/(24*60*60*1000));

      }
      catch(Exception e){
        System.out.println(“Error : Invalid Date “);
      }
  }

Running the above progrm
1) java DateDiff
Enter Date1
01/01/2011
Enter Date2
11/06/2011
Date1 = 01/01/2011
Date2 = 11/06/2011
Days between Date1 and Date2 = 161
2) java DateDiff
Enter Date1
10/06/2011
Enter Date2
11/06/2011
Date1 = 10/06/2011
Date2 = 11/06/2011
Days between Date1 and Date2 = 1
3) java DateDiff
Enter Date1
30/07/2011
Enter Date2
10/06/2011
Date1 = 30/07/2011
Date2 = 10/06/2011
Days between Date1 and Date2 = -50
———————————————————–
Method II :
1) Inputing two dates using Scanner Class with Delimiter “/” so that we can parse the date string to day , month , year integer.
2) Calender Class’s set method is used to convert into Date format using day, month, year , hour, min, second.
3) Calculate difference of two dates using Date getTime()
4) Calculate difference of two dates using the user defined function day();
import java.util.*;
import java.text.*;
import java.io.*;
public class DateDiffCal{
  public static void main(String args[]){

Calendar cal = Calendar.getInstance();
Calendar cal1 = Calendar.getInstance();
   
    int day1=0;
    int month1=0;
    int year1=0;
 
    int day2=0;  
    int month2=0;
    int year2=0;

try{

System.out.println(“Enter Date 1”);

Scanner s = new Scanner(System.in).useDelimiter(“[/\s]”);  //Input for Date 1
    
      day1= s.nextInt();
      month1 = s.nextInt();
      year1 = s.nextInt();

System.out.println(“Date1   : ” + day1 + “/” + month1 + “/”  + year1);

System.out.println(“Enter Date 2”);

 s = new Scanner(System.in).useDelimiter(“[/\s]”);  //Input for Date 2

    
      day2= s.nextInt();
      month2 = s.nextInt();
      year2 = s.nextInt();

System.out.println(“Date2   : ” + day2 + “/” + month2 + “/”  + year2);

   
} catch (Exception e)
  {

 System.out.println(“Error: Invalid Date”);
   }
 

     cal.set(year1 , month1-1, day1 , 12, 0,0);

     cal1.set(year2 , month2-1, day2, 12,0,0 );

long Days=(cal1.getTime().getTime()-cal.getTime().getTime())/(24*3600*1000);

System.out.println(“Number of Days between ” +   new SimpleDateFormat(“dd/MM/yyyy”).format(cal.getTime().getTime()) + ” and ” + new

SimpleDateFormat(“dd/MM/yyyy”).format(cal1.getTime().getTime()) + ” = ” + Days);

Days=days(cal,cal1);
System.out.println (“Days Calculated by  user defined function days() : “+Days);

}

public static long days(Calendar date1, Calendar date2) { 

Calendar temp;
boolean negative=false;
  long days = 0; 

if(date1.after(date2))                           //Swap date if date1 is greater than date2..
 {

 negative=true;
 temp=date1;
 date1=date2;
 date2=temp;
 }

  while (date1.before(date2))
       { 
    date1.add(Calendar.DAY_OF_MONTH, 1); 
    days++; 
        } 

if(negative)
 {
   return -days; 
 }
else
 {
 return days;
 }

}

Running the above Program
  
1) java DateDiffCal
Enter Date 1
01/01/2011
Date1 : 1/1/2011
Enter Date 2
11/06/2011
Date2 : 11/6/2011
Number of Days between 01/01/2011 and 11/06/2011 = 161
Days Calculated by user defined function days() : 161
2) java DateDiffCal
Enter Date 1
10/06/2011
Date1 : 10/6/2011
Enter Date 2
11/06/2011
Date2 : 11/6/2011
Number of Days between 10/06/2011 and 11/06/2011 = 1
Days Calculated by user defined function days() : 1
3) java DateDiffCal
Enter Date 1
30/07/2011
Date1 : 30/7/2011
Enter Date 2
10/06/2011
Date2 : 10/6/2011
Number of Days between 30/07/2011 and 10/06/2011 = -50
Days Calculated by user defined function days() : -50

You may also like

Leave a Reply