1360. Number of Days Between Two Dates

Write a program to count the number of days between two dates.

The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.

1
2
3
4
5
6
7
8
9
Example 1:

Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1

Example 2:

Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15

Constraints:

  • The given dates are valid dates between the years 1971 and 2100.

Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.time.*;

class Solution {
    
    public int daysBetweenDates(String date1, String date2) {
        return Math.abs(countDays(date1) - countDays(date2));
    }
    
    int [] MONTHS = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int countDays(String date) {
        String[] parts = date.split("-");
        Integer years = Integer.parseInt(parts[0]);
        Integer months = Integer.parseInt(parts[1]);
        Integer days = Integer.parseInt(parts[2]);
        
        for (int year = 1971; year < years; year++) {
            if (isLeapYear(year)) {
                days += 366;
            } else {
                days += 365;
            }
        }
        
        for (int month = 1; month < months; month++) {
            if (isLeapYear(years) && month == 2) {
                days += 29;
            } else {
                days += MONTHS[month];
            }
        }
        
        return days;
    }
    
    boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    
    public int daysBetweenDates2(String date1, String date2) {
        
        LocalDateTime time1 = LocalDate.parse(date1).atTime(LocalTime.NOON);
        LocalDateTime time2 = LocalDate.parse(date2).atTime(LocalTime.NOON);
        
        Duration duration = Duration.between(time1, time2);
        
        return (int) Math.abs(duration.toDays());
        
    }
}