Calculating days between is not working

public class MyDate {

`int year;`

`int month;`

`int day;`



`MyDate() {`

    `year = 2000;`

    `month = 1;`

    `day = 1;`

`}`



`MyDate(int year, int month, int day) {`

    `this.year = year;`

    `this.month = month;`

    [`this.day`](https://this.day) `= day;`

`}`



`public int getYear() {`

    `return year;`

`}`



`public int getMonth() {`

    `return month;`

`}`



`public int getDay() {`

    `return day;`

`}`



`public boolean isLeapYear() {`

    `if((year%400==0 || year%4==0) && (year % 100 != 0)) {`

        `return true;`

    `} else {`

        `return false;`

    `}`

`}`



`public boolean isLeapYear(int givenYear) {`

    `if((givenYear%400==0 || givenYear%4==0) && (givenYear % 100 != 0)) {`

        `return true;`

    `} else {`

        `return false;`

    `}`

`}`



`public int daysTo(MyDate other) {`



    `int[] months = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};`

    `int[] monthsLeap = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};`



    `int yearsToDaysThis = 0;`

    `for (int i = 0; i<=this.getYear(); i++) {`

        `if (isLeapYear(i)) {`

yearsToDaysThis += 366;

        `} else {`

yearsToDaysThis += 365;

        `}`

    `}`

    `int monthToDaysThis = 0;`

    `if (isLeapYear(this.getYear())) {`

        `for (int i = 0; i<=this.getMonth(); i++) {`

monthToDaysThis += monthsLeap[i];

        `}`

    `} else {`

        `for (int i = 0; i<=this.getMonth(); i++) {`

monthToDaysThis += months[i];

        `}`

    `}`



    `int totalDaysThis = yearsToDaysThis + monthToDaysThis +` [`this.day`](https://this.day)`;`



    `int yearsToDaysOther = 0;`

    `for (int i = 0; i<=other.getYear(); i++) {`

        `if (isLeapYear(i)) {`

yearsToDaysOther += 366;

        `} else {`

yearsToDaysOther += 365;

        `}`

    `}`



    `int monthToDaysOther = 0;`

    `if (isLeapYear(other.getYear())) {`

        `for (int i = 0; i<=other.getMonth(); i++) {`

monthToDaysOther += monthsLeap[i];

        `}`

    `} else {`

        `for (int i = 0; i<=other.getMonth(); i++) {`

monthToDaysOther += months[i];

        `}`

    `}`



    `int totalDaysOther = yearsToDaysOther + monthToDaysOther +` [`other.day`](https://other.day)`;`



    `return (Math.abs(totalDaysThis-totalDaysOther));`



`}`



`public static void main(String[] args) {`

    `MyDate date1 = new MyDate(2020,3,7);`

    `MyDate date2 = new MyDate(2020,4,1);`

    `System.out.println(date1.daysTo(date2));`

// System.out.println(date1.year + "/" + date1.month + "/" + date1.day);

// date1.addDays(21);

// System.out.println(date1.year + "/" + date1.month + "/" + date1.day);

// date1.addWeeks(3);

// System.out.println(date1.year + "/" + date1.month + "/" + date1.day);

`}`

}

/r/AskProgramming Thread