Working with Dates in Kotlin: Overview of Libraries and Tools
Date and time handling is one of the key tasks in any application: from event logging to calculating deadlines and displaying calendars. In Kotlin, as a language running on top of the JVM (and also supporting multiplatform projects), developers have access to several powerful libraries. In this article, we will look at three main approaches: kotlinx-datetime (recommended for multiplatform), java.time (for JVM/Android), and Joda-Time (a legendary library, relevant for legacy projects).
1. kotlinx-datetime — Official Multiplatform Library
kotlinx-datetime is a modern library from JetBrains, created specifically for Kotlin Multiplatform. It provides a unified API for working with dates, times, time zones, and durations on all platforms (JVM, JS, Native).
Installation (Gradle)
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.6.1")}Main Classes and Examples
Instant— a moment in time (UTC).LocalDateTime— date+time without a time zone.LocalDate— date only.LocalTime— time only.TimeZone— time zone.
import kotlinx.datetime.*
fun main() { // Current moment in UTC val now: Instant = Clock.System.now() println("Now UTC: $now")
// Conversion to local time (system time zone) val localDateTime: LocalDateTime = now.toLocalDateTime(TimeZone.currentSystemDefault()) println("Local: $localDateTime")
// Working with dates val today: LocalDate = localDateTime.date val tomorrow: LocalDate = today.plus(1, DateTimeUnit.DAY) println("Tomorrow: $tomorrow")
// Difference between dates val daysBetween = tomorrow.daysUntil(today) println("Days between: $daysBetween") // -1
// Parsing a string val parsedDate = LocalDate.parse("2025-03-15") println("Parsed: $parsedDate")}2. java.time (ThreeTen-Backport) — Standard for JVM and Android
Starting with Java 8, the java.time package (JSR-310) was included in the standard library. For Android API < 26, ThreeTen-Backport (or ThreeTenABP for Android) is used. Kotlin is fully compatible with this API.
Installation (for Android with minSdk < 26)
dependencies { implementation("org.threeten:threetenbp:1.6.9")}Examples
import java.time.*
fun main() { // Current date and time val now = LocalDateTime.now() println("Now: $now")
// Creating a specific date val date = LocalDate.of(2025, Month.DECEMBER, 25) println("Christmas: $date")
// Formatting val formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm") val formatted = now.format(formatter) println("Formatted: $formatted")
// Difference between dates val start = LocalDate.of(2025, 1, 1) val end = LocalDate.of(2025, 12, 31) val days = ChronoUnit.DAYS.between(start, end) println("Days in 2025: $days") // 364}3. Joda-Time — A Time-Tested Library
Joda-Time was the de facto standard before java.time appeared. Today it is mainly used in legacy projects. The library provides rich functionality but is considered outdated — migration to java.time or kotlinx-datetime is recommended.
Installation
dependencies { implementation("joda-time:joda-time:2.12.7")}Example
import org.joda.time.*import org.joda.time.format.DateTimeFormat
fun main() { val now = DateTime.now() println("Now: $now")
// Add