Prior to Kotlin 2.1, developers typically relied on the kotlinx-datetime library to work with time. This library provided utilities like Instant.now() and Clock.now() to retrieve the current time.
However, starting with Kotlin 2.1, the standard library (kotlin.time) includes a new Clock interface with a system implementation (Clock.System). The goal is to consolidate Kotlin’s time APIs into a single, built-in solution, removing duplication between kotlinx-datetime and kotlin.time.
As part of that transition:
- You now use
Clock.System.now()fromkotlin.time
Kotlin 2.1 introduced this new Clock interface with the following annotation:
@SinceKotlin("2.1")@ExperimentalTimepublic interface ClockHow to Get the Current Time
import kotlin.time.Clockimport kotlin.time.Instan
@OptIn(ExperimentalTime::class)val now = Clock.System.now()Make sure you’re importing the standard library types.
You do not need to include kotlinx-datetime unless you’re doing timezone conversions or working across JVM/JS/Native platforms.