Я рекомендую использовать java.time
для более точных результатов.
Вы можете реорганизовать свой код так:
fun main() {
var javaDate1 = java.time.LocalDate.of(2025,2,28)
var javaDate2 = java.time.LocalDate.of(2019,11,28).plusMonths(3)
var jd = java.time.Period.between(javaDate2, javaDate1)
println(
"Between ${javaDate2} and ${javaDate1} are ${jd.years} years, ${jd.months} months and ${jd.days} days")
javaDate1 = java.time.LocalDate.of(2025,2,28)
javaDate2 = java.time.LocalDate.of(2019,11,29).plusMonths(3)
jd = java.time.Period.between(javaDate2, javaDate1)
println(
"Between ${javaDate2} and ${javaDate1} are ${jd.years} years, ${jd.months} months and ${jd.days} days")
javaDate1 = java.time.LocalDate.of(2025,2,28)
javaDate2 = java.time.LocalDate.of(2019,11,30).plusMonths(3)
jd = java.time.Period.between(javaDate2, javaDate1)
println(
"Between ${javaDate2} and ${javaDate1} are ${jd.years} years, ${jd.months} months and ${jd.days} days")
}
запустите его и получите следующие результаты:
Between 2020-02-28 and 2025-02-28 are 5 years, 0 months and 0 days
Between 2020-02-29 and 2025-02-28 are 4 years, 11 months and 30 days
Between 2020-02-29 and 2025-02-28 are 4 years, 11 months and 30 days