-7

I have two date like this current date 2019-10-09 birthday date 2000-01-01

How can I recognize, that currentdate - birthdaydate will be more than 18 years ?

  • 5
    Possible duplicate of [Android difference between Two Dates](https://stackoverflow.com/questions/21285161/android-difference-between-two-dates) – Arbaz Pirwani Oct 09 '19 at 09:19
  • Possible duplicate of [How do I calculate someone's age in Java?](https://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java) – Ole V.V. Oct 10 '19 at 05:40

1 Answers1

0

Have a look at this question:

Finding days difference between dates in Android Kotlin.

Can easily be modified to get the year difference between dates.

import java.time.LocalDateTime
import java.time.temporal.ChronoUnit

fun main(args : Array<String>) {

    val start = "2019-05-24 14:17:00"
    val end = "2019-09-13 14:15:51"

    daysBetweenDates(start, end)
}


fun daysBetweenDates(start: String, end: String) {

    val format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

    val mStart = LocalDateTime.parse(start, format)
    val mEnd = LocalDateTime.parse(end, format)

    val difference = ChronoUnit.Years.between(mStart, mEnd)

    println("START => $mStart")
    println("END => $mEnd")
    println("DIFFERENCE => $difference in years"}
}
Olli
  • 658
  • 5
  • 26
  • 1
    Using java.time is good advice. Why not use `ChronoUnit.YEARS` from the outset? Converting by hand is a bad habit, and dividing by 365 doesn’t give an accurate result. – Ole V.V. Oct 10 '19 at 05:42