LeetCode in Kotlin

1360. Number of Days Between Two Dates

Easy

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.

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:

Solution

class Solution {
    fun daysBetweenDates(date1: String, date2: String): Int {
        val strings1 = date1.split("-").dropLastWhile { it.isEmpty() }.toTypedArray()
        val strings2 = date2.split("-").dropLastWhile { it.isEmpty() }.toTypedArray()
        return Math.abs(
            julianDay(strings1[0].toInt(), strings1[1].toInt(), strings1[2].toInt()) -
                julianDay(strings2[0].toInt(), strings2[1].toInt(), strings2[2].toInt()),
        )
    }

    private fun julianDay(year: Int, month: Int, day: Int): Int {
        val a = (14 - month) / 12
        val y = year + 4800 - a
        val m = month + 12 * a - 3
        return day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045
    }
}