2

I need to get the full days between two dates in java (the dates are given in Date type) .
For example: 01/01/2015/12:00:00 - 01/02/2015/11:59:00 isn't a full day and i need to consider daylight savings.
I know that jodatime lib does that but i reached the 65k method limit and i cant use jodatime lib.
i tried the millisecond diff way and the while loop that uses the "before" method: Android/Java - Date Difference in days

Community
  • 1
  • 1
maoz elbaz
  • 68
  • 10

2 Answers2

2

I manage to figure it out: i used some of this code - https://stackoverflow.com/a/28865648/3873513 and added some of mine:

     public static int calcDaysDiff(Date day1, Date day2) {
    Date d1 = new Date(day1.getTime());
    Date d2 = new Date(day2.getTime());
    Calendar date1 = Calendar.getInstance();
    date1.setTime(d1);
    Calendar date2 = Calendar.getInstance();
    date2.setTime(d2);
    //checks if the start date is later then the end date - gives 0 if it is
    if (date1.get(Calendar.YEAR) >= date2.get(Calendar.YEAR)) {
        if (date1.get(Calendar.DAY_OF_YEAR) >= date2.get(Calendar.DAY_OF_YEAR)) {
            return 0;
        }
    }
    //checks if there is a daylight saving change between the two dates

    int offset = calcOffset(d1, d2);

    if (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) {
        //swap them
        Calendar temp = date1;
        date1 = date2;
        date2 = temp;
    }

    return calcDaysDiffAux(date1, date2) + checkFullDay(date1, date2, offset);
}

// check if there is a 24 hour diff between the 2 dates including the daylight saving offset
public static int checkFullDay(Calendar day1, Calendar day2, int offset) {
    if (day1.get(Calendar.HOUR_OF_DAY) <= day2.get(Calendar.HOUR_OF_DAY) + offset) {
        return 0;
    }
    return -1;
}

// find the number of days between the 2 dates. check only the dates and not the hours
public static int calcDaysDiffAux(final Calendar day1, final Calendar day2) {
    Calendar dayOne = (Calendar) day1.clone(),
            dayTwo = (Calendar) day2.clone();

    if (dayOne.get(Calendar.YEAR) == dayTwo.get(Calendar.YEAR)) {
        return Math.abs(dayOne.get(Calendar.DAY_OF_YEAR) - dayTwo.get(Calendar.DAY_OF_YEAR));
    } else {

        int extraDays = 0;

        while (dayTwo.get(Calendar.YEAR) > dayOne.get(Calendar.YEAR)) {
            dayTwo.add(Calendar.YEAR, -1);
            // getActualMaximum() important for leap years
            extraDays += dayTwo.getActualMaximum(Calendar.DAY_OF_YEAR);
        }

        return extraDays - day1.get(Calendar.DAY_OF_YEAR) + day2.get(Calendar.DAY_OF_YEAR);
    }
}
Community
  • 1
  • 1
maoz elbaz
  • 68
  • 10
0
public class DateDiff {
  public static void main(String[] av) {
     SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy/HH:mm:ss");

     String inputString1 = "01/01/2015/12:00:00";
     String inputString2 = "01/02/2015/11:59:00";

     try {
         Date date1 = myFormat.parse(inputString1); 
         Date date2 = myFormat.parse(inputString2);

         long diff = date2.getTime() - date1.getTime(); // Calculate the different
         int days = (int) (diff / (1000*60*60*24)); // This convert milliseconds to days

         System.out.println ("Days differ: " + days);

     } catch (Exception e) {
         e.printStackTrace();
         }  
  }
}

The following code will calculate the two dates given, the result print is:

Days differ: 0
cw fei
  • 1,534
  • 1
  • 15
  • 33