LeetCode - Algorithms - 1360. Number of Days Between Two Dates

Problem

1360. Number of Days Between Two Dates

Java

Origin-based algorithm

© Algorithms for calculating the difference of dates in days - 4. Origin-based Algorithm

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
class Solution {
final static int[] daysUpToMonth = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
final static int[] daysUpToMonthLeapYear = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};

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

public int GetDaysOffsetFromOrigin(int year, int month, int day) {
if (isLeapYear(year)) {
year--;
int numOfLeapsYear = year / 4 - year / 100 + year / 400;
return year * 365 + numOfLeapsYear + daysUpToMonthLeapYear[month - 1] + day - 1;
} else {
year--;
int numOfLeapsYear = year / 4 - year / 100 + year / 400;
return year * 365 + numOfLeapsYear + daysUpToMonth[month - 1] + day - 1;
}
}

public int daysBetweenDates(String date1, String date2) {
int y1 = Integer.parseInt(date1.substring(0, 4));
int m1 = Integer.parseInt(date1.substring(5, 7));
int d1 = Integer.parseInt(date1.substring(8, 10));

int y2 = Integer.parseInt(date2.substring(0, 4));
int m2 = Integer.parseInt(date2.substring(5, 7));
int d2 = Integer.parseInt(date2.substring(8, 10));

int daysOffset = GetDaysOffsetFromOrigin(y1, m1, d1);
int daysOffset2 = GetDaysOffsetFromOrigin(y2, m2, d2);

int diff = daysOffset2 - daysOffset;

return diff >= 0 ? diff : -diff;
}
}

Submission Detail

  • 105 / 105 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Number of Days Between Two Dates.
  • Memory Usage: 37.1 MB, less than 71.69% of Java online submissions for Number of Days Between Two Dates.

jdk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import static java.time.temporal.ChronoUnit.DAYS;

class Solution {
public int daysBetweenDates(String date1, String date2) {
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate d1 = LocalDate.parse(date1, dft);
LocalDate d2 = LocalDate.parse(date2, dft);
long diff = DAYS.between(d1, d2);
int d = new Long(diff).intValue();
return d >= 0 ? d : -d;
}
}

Submission Detail

  • 105 / 105 test cases passed.
  • Runtime: 14 ms, faster than 13.23% of Java online submissions for Number of Days Between Two Dates.
  • Memory Usage: 38.4 MB, less than 21.16% of Java online submissions for Number of Days Between Two Dates.