2011-04-27 30 views
4

嗨,我問自己,如果有一個更簡單的方法來獲得兩個日期之間的天數。兩個日期之間的日期比較 - >更簡單的方法?

我只想要日子,不看時間或分鐘。

因此如果今天是星期一,日期至極我想比較於週三,天之間的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,並將to.getTime()設置爲date

編輯: 是否也可以與喬達時間,以獲取有關天信息,如:使用喬達時間後 差異== 1 ==>明天,或差異== -1 == >昨天 還是我必須手動做?

回答

7

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

+0

它支持日光保存太(拉爾夫說) – eav 2011-04-27 11:49:52

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

對不起,我不小心

+2

爲什麼你使用'double'和一個「浮動」常數?如果你使用'long',你不需要施放或調用'floor' – 2011-04-27 09:38:28

+1

是的,我會使用'long'並將其作爲一個常量。請注意,除了最初的內聯乘法之外,它會被解釋爲一個「int」。它在一天的時間間隔內工作正常,但是如果您在一個月內將「1000 * 60 * 60 * 24 * 30」除以後會溢出「int」,並且您不會得到預期的結果。 – WhiteFang34 2011-04-27 09:46:24

+0

謝謝大家,我想有一天我會贏得愚蠢的諾貝爾獎。 :)現在修復我的代碼 – 2011-04-27 09:51:55

1

相反都不初步認識值設置爲0,可以使用公郎DateUtils.truncate

反正dayDiff(開始 - 結束)/ milliesPerDay將無法工作正確,因爲Day Light保存更改。

+0

是的,我忘了... – eav 2011-04-27 09:52:53

7

對於指定的任務,我總是用這個方便的方法:(無lib中,不僅僅是Java 5 API)

import java.util.concurrent.TimeUnit; 

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

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

享受!

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

}

+0

我個人更喜歡使用現有的日期庫滾動我自己的。日期處理有很多細微之處,很容易忽略。 – Leigh 2012-06-14 07:58:53