1360. Number of Days Between Two Dates

```java
import java.text.SimpleDateFormat.*;
class Solution {
    int[] mdays = {0, 31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public int daysBetweenDates(String date1, String date2) {
        return Math.abs(getDays(date1) - getDays(date2));
    }
    private boolean isLeapYear(int y) {
        return y%4 == 0 && (y % 100 != 0 || y % 400 == 0);
    }
    private int getDays(String date) {
        int y = Integer.parseInt(date.substring(0, 4));
        int m = Integer.parseInt(date.substring(5, 7));
        int d = Integer.parseInt(date.substring(8, 10));

        int days = 0;
        for (int i = 1971; i < y; i++) {
            days += isLeapYear(i) ? 366 : 365;
        }
        for (int i = 1; i < m; i++) {
            days += mdays[i];
            if (i == 2 && isLeapYear(y)) {
                days++;
            }
        }
        days += d;

        return days;
    }
}

/*
just cal
*/
```

Last updated