Escriu per cercar…

Time

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() from kotlin.time

Kotlin 2.1 introduced this new Clock interface with the following annotation:

kotlin
@SinceKotlin("2.1")
@ExperimentalTime
public interface Clock

How to Get the Current Time

kotlin
import kotlin.time.Clock
import 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.

Estàs llegint una vista prèvia.

Inicia sessió amb Google per llegir la pàgina completa.

Inicia sessió amb Google

Amb qualsevol compte de Google. Només et demanarem que acceptis les condicions del servei.