Introduction to working with dates and time in Kotlin
Working with dates and time is one of the key tasks in developing any application. Whether it's event logging, calculating deadlines, displaying a calendar, or working with timestamps, you can't do without a reliable toolkit. In the Kotlin ecosystem, the powerful java.time library is used for these purposes, which replaced the outdated java.util.Date and java.util.Calendar. Thanks to full compatibility with Java, Kotlin allows you to use all the features of java.time directly, and also adds convenient extensions and syntactic sugar.
In this article, we will take a detailed look at how to create, format, parse, and convert dates and time in Kotlin. You will learn about key classes such as LocalDate, LocalTime, LocalDateTime, ZonedDateTime, and Instant, and also learn how to work with periods and durations. All code examples are written in pure Kotlin and are ready to use.
Main classes for working with dates and time
The java.time library offers several key classes, each of which solves its own task. Let's look at them in more detail.
LocalDate — working with date without time
The LocalDate class represents a date (year, month, day) without considering time and time zone. It is ideal for birthdays, calendar events, and tasks where time does not matter.
import java.time.LocalDate
fun main() { // Current date val today = LocalDate.now() println("Today: $today")
// Specific date val specificDate = LocalDate.of(2024, 12, 25) println("Christmas: $specificDate")
// Parsing from string val parsedDate = LocalDate.parse("2024-12-25") println("Parsed: $parsedDate")}LocalTime — working with time without date
LocalTime stores only time (hours, minutes, seconds, nanoseconds). It is used for schedules, timers, and timestamps without binding to a date.
import java.time.LocalTime
fun main() { // Current time val now = LocalTime.now() println("Now: $now")
// Specific time val lunchTime = LocalTime.of(13, 30) println("Lunch at: $lunchTime")
// Parsing val parsedTime = LocalTime.parse("15:45:30") println("Parsed: $parsedTime")}LocalDateTime — combining date and time
LocalDateTime combines LocalDate and LocalTime. This is the most commonly used class for storing a point in time without considering the time zone.
import java.time.LocalDateTimeimport java.time.LocalDateimport java.time.LocalTime
fun main() { // Current moment val now = LocalDateTime.now() println("Now: $now")
// From individual components val date = LocalDate.of(2024, 12, 25) val time = LocalTime.of(10, 0) val christmasMorning = LocalDateTime.of(date, time) println("Christmas morning: $christmasMorning")
// Combined method val newYear = LocalDateTime.of(2025, 1, 1, 0, 0) println("New Year: $newYear")}Formatting and parsing dates
To convert dates to strings and back, the DateTimeFormatter class is used. It supports many predefined formats and also allows you to create custom patterns.
Formatting a date into a string
import java.time.LocalDateTimeimport java.time.format.DateTimeFormatter
fun main() { val now = LocalDateTime.now()
// Standard ISO format val isoFormat = now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) println("ISO: $isoFormat")
// Custom format val customFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss") va