// build.gradle.kts:
// implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.+")
import kotlinx.datetime.*
fun main() {
// Now — as UTC instant
val now: Instant = Clock.System.now()
println(now) // 2025-03-12T19:25:00Z
// Local date in a specific timezone
val tz = TimeZone.of("America/Chicago")
val localNow: LocalDateTime = now.toLocalDateTime(tz)
println(localNow) // 2025-03-12T14:25:00.123
// Date-only
val today: LocalDate = localNow.date
println(today.year) // 2025
println(today.dayOfWeek) // WEDNESDAY
// Add / subtract — DateTimePeriod for calendar units
val nextWeek = today.plus(DatePeriod(days = 7))
val twoMonthsAgo = today.minus(2, DateTimeUnit.MONTH)
println("$nextWeek, $twoMonthsAgo")
// Duration between two Instants
val later = now.plus(1, DateTimeUnit.HOUR, tz)
val elapsed = later - now // kotlin.time.Duration
println(elapsed) // 1h
}
Create a free account and build your private vault. Share publicly whenever you want.