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 made this logic ... and it does not work. This returns the wrong answer. Can anyone help with the logic?

+2
source share
4 answers

Using Joda Date and Time: -

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

If you have two date objects, it is much easier to subtract milliseconds:

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

And then convert the time difference to the day difference:

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

Note: this only works with dates from January 1, 1970.

If you try to reproduce the entire logic of the calendar yourself (for example, leap years), you have a good chance that you will make a mistake. There are an amazing amount of subtle corner cases to bite you, and others have already figured it all out.

Java, . JODA: http://joda-time.sourceforge.net/

+1

Your logic for determining the number of leap years is incorrect. See the answer from Alok on this question:

How to programmatically select a leap year in C

0
source

If you are going to deal with dates between 1900 and 2100, there is a simple calculation that will give you the number of days since 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, in order to get the difference in days between two dates, you calculate your days since 1900 and calculate the difference. Our daysBetween method is as follows:

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 do not ask me where this calculation came from, because we used it from the beginning of the 90s.

0
source

Source: https://habr.com/ru/post/1796226/


All Articles