Kotlin's Duration
✦ 2025-07-15
Just a quick note to say that Kotlin’s Duration support awesome.
- ⏱️ Extension properties for Double, Int, and Long:
Use something like
1.daysor2.hoursto produce a Kotlin duration. It’s more expressive than Java’sDuration.ofDays(1)orDuration.ofhours(2). - 📝 Retrieve an ISO8601-compatible duration string:
Call
toIsoString()and you’ll get something like"PT1D"or"PT2H". - 🔢 Convert to whole milliseconds, seconds, etc:
Use properties like
inWholeMillisecondsorinWholeSecondsto conver toLongrepresentations when you need them. - 📐 Mathematical operations work how you’d expect:
1.day + 2.hours + 3.minutesdoes what you’d expect. So does1.day > 2.hours.
Reminds me a lot of ActiveSupport (archive). For more, read Duration’s Companion (archive) object or the Duration (archive) class itself in the Kotlin documentation.