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;
6
  • 3
    so whats your question ? Commented Apr 18, 2014 at 9:27
  • I would suggest use list collections to store your date array. Then do a list.sort() on it. Here is how you do it. Commented Apr 18, 2014 at 9:31
  • you want to get the days between the dates ? Commented Apr 18, 2014 at 9:40
  • yes, i need to return an int containing the largest span between dates in the array (in days). Commented Apr 18, 2014 at 9:42
  • @faizanjehangir, the values inside of the Date[] have already been sorted Commented Apr 18, 2014 at 9:44

2 Answers 2

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 :)

Sign up to request clarification or add additional context in comments.

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.