简体   繁体   English

找到两个给定日期的天数差异

[英]finding the difference in days from two given dates

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

I have made this logic ...and it's not working. 我已经提出了这个逻辑...但它不起作用。 It's returning the wrong answer. 返回错误的答案。 Can anybody help in the logic? 有人可以提供逻辑帮助吗?

Using Joda Datetime:- 使用Joda日期时间:-

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

If you have two date objects, it's much simpler to subtract the millisecond times: 如果您有两个日期对象,则减去毫秒时间要简单得多:

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

And then convert the time difference to a day difference: 然后将时差转换为日差:

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

Note: this only works for dates since Jan 1, 1970 注意:这仅适用于1970年1月1日以后的日期

If you try to replicate all the calendar logic yourself (eg leap years), there's a good chance you'll get it wrong. 如果您尝试自己复制所有日历逻辑(例如leap年),则很有可能会弄错它。 There are a surprising number of subtle corner cases to bite you, and others have already figured it all out. 有很多令人难以置信的微妙情况会咬你,其他人已经弄清楚了。

And if you need serious multi-calendar Java date handling, see JODA: http://joda-time.sourceforge.net/ 如果您需要认真的多日历Java日期处理,请参见JODA: http ://joda-time.sourceforge.net/

Your logic to determine the number of leap years is incorrect. 您确定of年数的逻辑不正确。 See the answer from Alok on this question: 请参阅Alok关于此问题的答案:

How to find leap year programatically in C 如何在C中以编程方式查找leap年

If you are only going to be dealing with dates between the years 1900 and 2100, there is a simple calculation which will give you the number of days since 1900: 如果您仅要处理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);
}

Thus, to get the difference in days between two dates, you calculate their days since 1900 and calc the difference. 因此,要获取两个日期之间的天数差异,您可以计算它们自1900年以来的天数并计算差异。 Our daysBetween method looks like this: 我们的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;
    }
}

And don't ask me where this calculation came from because we've used it since the early '90s. 而且不要问我这个计算的来源,因为我们从90年代初就开始使用它。

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

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