Wrangling Dates & Time in Android

Dec 15 2022 · Kotlin 1.6.21, Android 13, IntelliJ 2022.1

Part 1: Wrangling Dates & Time in Android

03. Use Duration, Period & ChronoUnit to Calculate Time

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Work With Instant Next episode: 04. Store Dates & Times Using Date Classes

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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)
println(
    "You are ${ageWithPeriod.years} years, " +
      "${ageWithPeriod.months} moths " +
      "and ${ageWithPeriod.days} days old"
  )
val ageWithChrono = ChronoUnit.DAYS.between(birthDay, today)
println("ageWithChrono: $ageWithChrono days")