2011-02-13 57 views
2
/** Determines the difference in days between d and this Date.For example, 
    * if this Date is 12/15/1997 and d is 12/14/1997, the difference is 1. 
    * If this Date occurs before d, the result is negative. 
    * @return the difference in days between d and this date. 
    */ 

public int difference(Date d) { 
int NoOfLeapYr_d = d.year/4;  
int NoOfLeapYr_this = this.year/4; 
int daysofthis = NoOfLeapYr_this + (this.year-1 * 365) + this.dayInYear(); 
int daysofd = NoOfLeapYr_d + (d.year-1 * 365) + d.dayInYear(); 
    return daysofd - daysofthis;       
} 

我已經完成了這個邏輯......並且它不工作。它返回了錯誤的答案。任何人都可以幫助邏輯嗎?發現兩個給定日期的差異

+1

什麼不起作用?你能舉一些例子輸入,輸出和期望的輸出嗎? – 2011-02-13 14:32:16

回答

1

如果有兩個日期的對象,這是更簡單減去毫秒時間:

long diff = today.getTime() - d1.getTime(); 

然後時差轉換爲天差:

long days_diff = diff/(1000*60*60*24); 

注:這僅適用於自1970年1月1日以來的日期

如果您嘗試自己複製所有日曆邏輯(例如閏年),那麼很可能會出錯。有一個令人驚訝的數量微妙的角落案件咬你,而其他人已經把它全部弄清楚了。

如果你需要認真多日曆的Java日期處理,請參閱JODA:http://joda-time.sourceforge.net/

+0

如果兩次來自不同的時間,這可能不會產生預期的結果。 – 2011-02-13 14:41:05

+0

是的。通過將getTime()值四捨五入到最近的一天,可以輕鬆解決這個問題。 – payne 2011-02-13 16:19:28

3

使用喬達日期時間: -

@Test 
public void testOneDayEarlier() { 
    DateTime fromDate = new DateTime(2011, 2, 12, 0, 0, 0, 0); 
    DateTime toDate = new DateTime(2011, 2, 13, 0, 0, 0, 0); 

    int days = Days.daysBetween(fromDate, toDate).getDays(); 
    assertEquals("fromDate is one day earlier than toDate", 1, days); 
} 

@Test 
public void testOneDayLater() { 
    DateTime fromDate = new DateTime(2011, 2, 13, 0, 0, 0, 0); 
    DateTime toDate = new DateTime(2011, 2, 12, 0, 0, 0, 0); 

    int days = Days.daysBetween(fromDate, toDate).getDays(); 
    assertEquals("fromDate is one day later than toDate", -1, days); 
} 

@Test 
public void testSameDay() { 
    DateTime fromDate = new DateTime(2011, 2, 13, 0, 0, 0, 0); 
    DateTime toDate = new DateTime(2011, 2, 13, 0, 0, 0, 0); 

    int days = Days.daysBetween(fromDate, toDate).getDays(); 
    assertEquals("fromDate is the same as toDate", 0, days); 
} 
0

如果你只打算來對付年1900和2100之間的日期,還有一個簡單的計算,這將給你自1900年以來的天數:

public static int daysSince1900(Date date) { 
    Calendar c = new GregorianCalendar(); 
    c.setTime(date); 

    int year = c.get(Calendar.YEAR); 
    if (year < 1900 || year > 2099) { 
     throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099"); 
    } 
    year -= 1900; 
    int month = c.get(Calendar.MONTH) + 1; 
    int days = c.get(Calendar.DAY_OF_MONTH); 

    if (month < 3) { 
     month += 12; 
     year--; 
    } 
    int yearDays = (int) (year * 365.25); 
    int monthDays = (int) ((month + 1) * 30.61); 

    return (yearDays + monthDays + days - 63); 
} 

因此,爲了得到兩個日期之間的天數的不同,你算算兵衛1900年以來的天數並計算差異。我們daysBetween方法是這樣的:

public static Integer getDaysBetween(Date date1, Date date2) { 
    if (date1 == null || date2 == null) { 
     return null; 
    } 

    int days1 = daysSince1900(date1); 
    int days2 = daysSince1900(date2); 

    if (days1 < days2) { 
     return days2 - days1; 
    } else { 
     return days1 - days2; 
    } 
} 

不要問我在哪裏,這個計算從因爲我們自從90年代初用它來了。