Wrangling Dates & Time in Android

Dec 15 2022 Kotlin 1.6.21, Android 13, IntelliJ 2022.1

Part 1: Wrangling Dates & Time in Android

3. Use Duration, Period & ChronoUnit to Calculate Time

Episode complete

Play next episode

Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 2. Work With Instant Next episode: 4. Store Dates & Times Using Date Classes

This video Use Duration, Period & ChronoUnit to Calculate Time was last updated on Dec 15 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

When you write code to specify an amount of time, you should use the class or method that best meets your needs. In this lecture, you’ll see three different classes: Period, Duration, and ChronoUnit.

val betweenWithDuration = Duration.between(Instant.EPOCH, Instant.now())
val daysInBetweenWithDuration = betweenWithDuration.toDays()
val birthDay = LocalDate.of(1999, Month.DECEMBER, 29)
val today = LocalDate.now()
val ageWithPeriod = Period.between(birthDay, today)
    "You are ${ageWithPeriod.years} years, " +
      "${ageWithPeriod.months} moths " +
      "and ${ageWithPeriod.days} days old"
val ageWithChrono = ChronoUnit.DAYS.between(birthDay, today)
println("ageWithChrono: $ageWithChrono days")