0

I have to sort through a Date[] and calculate what the largest span between entries is in days. The values are parsed to Date from string values (being passed from the database) An example date array would look like so:

Date[] myDateArray = {01/01/2014,03/01/2014,04/01/2014,07/01/2014,19/01/2014};

This is a snippet of my method.

temp = i_value = j_value = max = maxdiff = diff = 0;

for (int i = 0; i < dateValues.length; i++) {
    for (int j = i + 1; j < dateValues.length; j++) {
        cal.setTime(dateValues[i]);
        i_value = (cal.get(Calendar.YEAR) * 365) + cal.get(Calendar.DAY_OF_YEAR);
        cal.setTime(dateValues[j]);
        j_value = (cal.get(Calendar.YEAR) * 365) + cal.get(Calendar.DAY_OF_YEAR);
        max = Math.abs(i_value - j_value);
    }
    diff = Math.abs(max - max2);
    if (maxdiff < diff) {
        maxdiff = diff;
    }
    temp = maxdiff;
}
return temp;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
The_Doctor
  • 181
  • 5
  • 16

2 Answers2

1

The below code will give you days between the two dates.

  public  int getDaysBetween(Date date1, Date date2) {

    Calendar d1 = Calendar.getInstance();
    Calendar d2 = Calendar.getInstance();
    d1.setTime(date1);
    d2.setTime(date2);

    int days = d2.get(Calendar.DAY_OF_YEAR)
            - d1.get(Calendar.DAY_OF_YEAR);
    int y2 = d2.get(Calendar.YEAR);
    try {
        if (d1.get(Calendar.YEAR) < y2) {
            d1 = (Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        } else if (d1.getCalendar.YEAR) > y2) {
            d1 = (Calendar) d1.clone();
            do {
                days -= d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, -1);
            } while (d1.get(Calendar.YEAR) != y2);
            if ((y2 % 4) == 0) {
                days -= 1;
            }
        }

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

    }

    return days+1;
}

hope this will help you :)

Gundamaiah
  • 780
  • 2
  • 6
  • 30
0

This code doesn't use Calendar.

/**
 * Ascertain the largest span between dates of a sorted array in days.
 * @param sortedDateArray Sorted array of java.util.Dates without nulls
 * @return largest span between two batched dates in sortedDateArray in days
 */
public static int largestSpan(Date[] sortedDateArray) {
    int maxDiffDays = 0;
    for (int i = 0, j = 1; i < sortedDateArray.length && j < sortedDateArray.length; i = j++) {
        Date b = sortedDateArray[i + 1];
        Date a = sortedDateArray[i];
        long diff = b.getTime() - a.getTime(); // calculates the difference between to batched dates (a and b) in ms
        int diffDays = (int) (diff / (1000 * 60 * 60 * 24)); // converts the difference from ms to days

        System.out.println(diffDays + " days between " + a.toString()
                + " and " + b.toString()); // prints out the difference in days (just for debugging)

        if (diffDays > maxDiffDays)
            maxDiffDays = diffDays; // sets the difference as new maximum difference if it is larger than the previous value
    }
    return maxDiffDays; // returns the largest difference in days
}
Marcel Binder
  • 166
  • 10