两个日期之间的天数比较 - >更方便吗?

时间:2011-04-27 09:14:50

标签: java date comparison calendar

嗨,我问自己是否有更简单的方法来获得两个日期之间的天数。

我只想要几天,而不是看小时或分钟。

因此,如果今天是星期一,我要比较的日期是星期三,两天之间的日子是2(时间无关紧要)

因此我使用此代码:

        Calendar c = Calendar.getInstance();
        // Only the day:
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        Calendar to = Calendar.getInstance();
        to.setTime(date);
        to.set(Calendar.HOUR, 0);
        to.set(Calendar.MINUTE, 0);
        to.set(Calendar.SECOND, 0);
        to.set(Calendar.MILLISECOND, 0);
        date = to.getTime();

        long millsPerDay = 1000 * 60 * 60 * 24;

        long dayDiff = ( date.getTime() - dateToday.getTime() ) / millsPerDay;

在这段代码之后我有一个叫做dayDiff的日子。 但是真的必须制作日期日历,将时间设置为00:00:00:00并将to.getTime()保存在date中吗?

编辑:使用joda-time后: 是否有可能通过joda-time获取有关日期的信息,例如: 差异== 1 ==>明天或差异== -1 ==>昨天 或者我必须手动完成吗?

5 个答案:

答案 0 :(得分:7)

您可以使用JodaTime所示的here API。

答案 1 :(得分:7)

对于指定的任务,我总是使用这种方便的方法:(没有lib,只是Java 5 API)

import java.util.concurrent.TimeUnit;

Date d1 = ...
Date d2 = ...

long daysBetween = TimeUnit.MILLISECONDS.toDays(d2.getTime() - d1.getTime());

享受!

答案 2 :(得分:3)

public long dayDiff(Date d1, Date d2) {
    final long DAY_MILLIS = 1000 * 60 * 60 * 24;
    long day1 = d1.getTime() / DAY_MILLIS;
    long day2 = d2.getTime() / DAY_MILLIS;
    return (day1 - day2);
}

对不起我的粗心

答案 3 :(得分:1)

您可以使用commons lang DateUtils.truncate

而不是将所有非关联值设置为0

无论如何,dayDiff(start-end)/ milliesPerDay将无法正常工作,因为Day Light Save更改。

答案 4 :(得分:1)

这是一种不基于危险毫秒转换的分析daydiff方法:

public static int dayDiff(Calendar to, Calendar from){
    int result = 0;
    int years;


    // global year difference from 1.jan to 1.jan
    years = to.get(Calendar.YEAR) - from.get(Calendar.YEAR);
    result = years * 365; 

    // adding days for simple leap years ( divisible by 4 ). This an approximation that will be corrected by the negative leap years formula.
    result += (to.get(Calendar.YEAR)-1)/4 - (from.get(Calendar.YEAR)-1)/4;

    // removing days for negative leap years ( divisible by 100 ). This is still an approximation that will be corrected by the big leap years formula.
    result -= (to.get(Calendar.YEAR)-1)/100 - (from.get(Calendar.YEAR)-1)/100;

    // adding days for big leap years ( divisible by 400 ). After this formula, the days count from 1.jan.<from> to 1.jan.<to> is correct.
    result += (to.get(Calendar.YEAR)-1)/400 - (from.get(Calendar.YEAR)-1)/400;

    // adding month of to-year
    for(int m=0; m<to.get(Calendar.MONTH ); m++){
        result += daysInMonth(m, to.get(Calendar.YEAR));
    }

    // substracting month of from-year
    for(int m=0; m<from.get(Calendar.MONTH ); m++){
        result -= daysInMonth(m, from.get(Calendar.YEAR));
    }

    // adding days of to-year
    result += to.get(Calendar.DAY_OF_MONTH ); 


    // substracting days of from-year
    result -= from.get(Calendar.DAY_OF_MONTH ); 

    return result;

}

private static int daysInMonth(int m, int y){
    if(m==3 || m==5 || m==8 || m==10) return 30;
    if(m==1)
        if(isLeapYear(y)) return 29;
        else return 28;
    return 31;
}


private static boolean isLeapYear(int y){
    return (isSimpleLeapYear(y) && !isNegativeLeapYear(y)) || isBigLeapYear(y);
}

private static boolean isSimpleLeapYear(int y){
    return y%4 == 0;
}

private static boolean isNegativeLeapYear(int y){
    return y%100 == 0;
}

private static boolean isBigLeapYear(int y){
    return y%400 == 0;
}

}