Calculate Number of Days Between Two Dates

Calculate Number of Days Between Two Dates

To calculate number of days between two dates, we can use Duration.between() method. In the code below, dates are passed as strings, like 2012-12-30.

import java.time.Duration
import java.time.LocalDate

fun daysBetweenDates(date1: String, date2: String): Int {
    return Math.abs(

The usage of atStartOfDay() is required, otherwise it fails with UnsupportedTemporalTypeException.

Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Seconds
	at java.time.LocalDate.until(
	at java.time.Duration.between(
© 2021 VividCode