繁体   English   中英

数组中日期之间的差异

[英]difference between dates in array

我必须对Date []进行排序,然后计算输入之间最大的跨度(以天为单位)。 从字符串值(从数据库传递)将这些值解析为Date,示例日期数组如下所示:

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

这是我的方法的摘要。

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;

以下代码将为您提供两个日期之间的天数。

  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;
}

希望这个能对您有所帮助 :)

此代码不使用日历。

/**
 * 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM