2 approaches to check whether difference between given two dates is greater than specified number of days or not in java



Contents of page :
  • Program/Approach 1 to  check whether difference between given two dates is greater than 3 days or not ?
  • Program/ Approach 2 to  check whether difference between given two dates is greater than 3 days or not ?



Program/Approach 1 to  check whether difference between given two dates is greater than 3 days or not ?


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... a) {
          //1. Forming manual date
          Date date1 = null;
          Date date2 = null;
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 date1 = sdf.parse("11-02-2015");
                 date2 = sdf.parse("23-02-2015");
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
          //2. Forming GregorianCalendar from manual date
          GregorianCalendar cal1 = new GregorianCalendar();
          GregorianCalendar cal2 = new GregorianCalendar();
          cal1.setTime(date1);
          cal2.setTime(date2);
         
          //----------------------------Now, starts actual LOGIC------------------
         
         
          //3. LOGIC for finding difference two dates is greater than 3 days or not
         
          long diffIn_MilliSec = Math.abs(cal1.getTimeInMillis() - cal2.getTimeInMillis()); //abs have been used to avoid negative results
          if(diffIn_MilliSec / (1000 * 60 * 60 * 24) > 3){
                 System.out.println("difference two date1 and date2 is greater than 3 days");
          } else{
                 System.out.println("difference two date1 and date2 is less than 3 days");
          }
         
   }
}
/*OUTPUT
difference two date1 and date2 is greater than 3 days
*/



Program/ Approach 2 to  check whether difference between given two dates is greater than 3 days or not ?

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class DateTest {
   public static void main(String... a) {
          //1. Forming manual date
          Date date1 = null;
          Date date2 = null;
          try {
                 SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
                 date1 = sdf.parse("11-02-2015");
                 date2 = sdf.parse("23-02-2015");
          } catch (ParseException pe) {
                 pe.printStackTrace();
          }
          //2. FormingGregorianCalendar from manual date
          GregorianCalendar cal1 = new GregorianCalendar();
          GregorianCalendar cal2 = new GregorianCalendar();
          cal1.setTime(date1);
          cal2.setTime(date2);
         
          //----------------------------Now, starts actual LOGIC------------------
         
          //Add 3 days to date
          cal1.add(Calendar.DATE, 3);
         
          //Now, find out whether cal1 is before cal2 or not
          //Example >
                 //Adding 3 days to 11-02-2015 will make it 14-02-2015,
                 //So cal1 (i.e. 14-02-2015) is before date2 (i.e. 23-02-2015)
          if(cal1.before(cal2)){
                 System.out.println("difference two dates is greater than 3 days");
          } else{
                 System.out.println("difference two dates is less than 3 days");
          }
         
   }
}
/*OUTPUT
difference two dates is greater than 3 days
*/



RELATED LINKS>
Creating Date >

Creating Date in java using Calendar, GregorianCalendar and java.util.Date | Getting Year, Month, Date, Hour, Minute, Second from current date



Creating Date manually - by passing users parameter>

Forming date MANUALLY using java.util.Date’s constructor, GregorianCalendar’s constructor and set method in java


Difference between two Dates>

Difference between two dates in days, hours, minutes, seconds, milliSeconds- where dates are manually created using dd-MM-yyyy and dd-MM-yyyy hh:mm:ss format


eEdit
Must read for you :