0

Output between 15/02/2020 and 02/03/2020 produces 18, but I expect 16.

What am I doing wrong?

public void checkNumberOfDaysBetweenTwoDates(models.Date date2) {
    int day_diff, mon_diff, year_diff;

    if(date2.checkIfLegal()) {
        if(date2.day < day) {
            date2.day += date2.numberOfDaysInMonth();
        }
        date2.month = date2.month - 1;
    }

    if(date2.month < month) {
        date2.month += 12;
        date2.year -= 1;
    }

    day_diff = date2.day - day;
    mon_diff = date2.month - month;
    year_diff = date2.year - year;

    System.out.println("days: " + day_diff + "months: " + mon_diff + "years: " + year_diff);
}

private boolean isLeapYear() {
    return year % 4 == 0 &&
        (year % 100 != 0 || year % 400 == 0);
}

public boolean checkIfLegal() {
    boolean validation = false;

    if(month < 1 || month > 12 || day < 1 || day > numberOfDaysInMonth()) {
        validation = false;
    } else {
        validation = true;
    }

    return validation;
}

private int numberOfDaysInMonth() {
    if (month == 2 && isLeapYear())
        return 29;
    else
        return daysInMonth[month - 1];
}
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
TiKi
  • 27
  • 3
  • Did you check https://stackoverflow.com/questions/20165564/calculating-days-between-two-dates-with-java ? – Arvind Kumar Avinash Feb 14 '20 at 12:18
  • @ArvindKumarAvinash those solutions include different java methods like getTime(), Calendar and other stuff. I want the mathematical procedure of the calculation between 2 dates. – TiKi Feb 14 '20 at 12:26
  • 1
    @TiKi, first of all, it's not right to change input argument. You shoudn't alter `date2` object in computation. As for what's wrong, debug it: when `date2.day < day`, you add to `date2.day` number of days in March, which is 31, and it's exactly 2 greater than number of days in February this year, and has good correlation with your error margin. – M. Prokhorov Feb 14 '20 at 12:32
  • It is most comfortable to use the java.time API, which provides ready-made implementations in classes like `ZonedDateTime`. – pxcv7r Feb 14 '20 at 12:37
  • 1
    @pxcv7r, in this particular case it's `LocalDate` and `Period`, but that's not the point of the question: OP wants to write his own implementation. – M. Prokhorov Feb 14 '20 at 12:40

1 Answers1

1

You add the number of days in a month if the day of the second date is less than the day of the first date:

date2.day += date2.numberOfDaysInMonth();

You add the number of days in the month of date2 (here 31, the number of days in March). But you must add the number of days in the previous month (here 29, the number of days in February).

Another thing to consider: Method checkIfLegal can be simplified to

public boolean checkIfLegal() {
    return month >= 1 && month <= 12 && day >= 1 && day <= numberOfDaysInMonth();
}

And the method should have a parameter which defines the date to check. Do not use a global variable here!

Donat
  • 4,157
  • 3
  • 11
  • 26