Skip to content

Commit f122cd6

Browse files
solves number of days between dates
1 parent a4e1a10 commit f122cd6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
| 1346 | [Check if N and It's Double Exist](https://leetcode.com/problems/check-if-n-and-its-double-exist) | [![Java](assets/java.png)](src/CheckIfNAndItsDoubleExist.java) [![Python](assets/python.png)](python/check_if_n_and_its_double_exist.py) | |
348348
| 1351 | [Count Negative Numbers In A Sorted Matrix](https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix) | [![Java](assets/java.png)](src/CountNegativeNumbersInSortedMatrix.java) | |
349349
| 1356 | [Sort Integers by Number of 1 Bits](https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits) | [![Java](assets/java.png)](src/SortIntegersByTheNumberOf1Bits.java) | |
350-
| 1360 | [Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates) | | |
350+
| 1360 | [Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates) | [![Java](assets/java.png)](src/NumberOfDaysBetweenTwoDates.java) | |
351351
| 1365 | [How Many Numbers Are Smaller Than Current Number](https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number) | | |
352352
| 1370 | [Increasing Decreasing String](https://leetcode.com/problems/increasing-decreasing-string) | | |
353353
| 1374 | [Generate A String With Characters That Have Odd Count](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts) | | |

src/NumberOfDaysBetweenTwoDates.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.time.LocalDate;
2+
3+
import static java.time.temporal.ChronoUnit.DAYS;
4+
5+
public class NumberOfDaysBetweenTwoDates {
6+
public int daysBetweenDates(String date1, String date2) {
7+
final String[] dateParts1 = date1.split("-");
8+
final String[] dateParts2 = date2.split("-");
9+
final LocalDate d1 = LocalDate.of(
10+
Integer.parseInt(dateParts1[0]),
11+
Integer.parseInt(dateParts1[1]),
12+
Integer.parseInt(dateParts1[2])
13+
);
14+
final LocalDate d2 = LocalDate.of(
15+
Integer.parseInt(dateParts2[0]),
16+
Integer.parseInt(dateParts2[1]),
17+
Integer.parseInt(dateParts2[2])
18+
);
19+
return (int) Math.abs(DAYS.between(d1, d2));
20+
}
21+
}

0 commit comments

Comments
 (0)