Working with Dates and Time in Kotlin: Advanced Techniques and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Advanced Work with Dates and Time in Kotlin

\n\n

In modern Kotlin development, proper handling of dates and time is not just a convenience, but a necessity. At an advanced level, a developer has to deal with time zones, periods, durations, and complex formatting operations. In this article, we will look at the key tools and techniques that will help you write reliable and efficient code.

\n\n

1. Core Classes: LocalDate, LocalTime, LocalDateTime, and Instant

\n\n

Kotlin fully inherits the powerful java.time library (JSR-310), which has become the standard for working with dates and time in Java 8 and above. For Kotlin Multiplatform, there is the kotlinx-datetime library, but within JVM projects, java.time remains the most popular.

\n\n

LocalDate, LocalTime, LocalDateTime

\n

These classes represent date and time without a time zone. They are ideal for storing birthdays, schedules, and other data where the time zone is not important.

\n\n
import java.time.LocalDate\nimport java.time.LocalTime\nimport java.time.LocalDateTime\n\nfun main() {\n    val date = LocalDate.now()\n    val time = LocalTime.of(14, 30, 0)\n    val dateTime = LocalDateTime.of(date, time)\n    \n    println("Date: $date")\n    println("Time: $time")\n    println("Date and Time: $dateTime")\n    \n    // Adding and subtracting days\n    val tomorrow = date.plusDays(1)\n    println("Tomorrow: $tomorrow")\n}
\n\n

Instant — Timestamp for Machines

\n

Instant represents a point on the time scale in UTC. It is indispensable for logging, data exchange between servers, and working with APIs.

\n\n
import java.time.Instant\n\nfun main() {\n    val now = Instant.now()\n    println("Current moment in UTC: $now")\n    \n    // Conversion from milliseconds\n    val fromEpoch = Instant.ofEpochMilli(1_700_000_000_000)\n    println("From milliseconds: $fromEpoch")\n}
\n\n

2. Time Zones: ZonedDateTime and OffsetDateTime

\n\n

When an application works with users from different regions, it is necessary to consider time zones. For this, there are ZonedDateTime (with full zone information) and OffsetDateTime (only the offset from UTC).

\n\n
import java.time.ZonedDateTime\nimport java.time.ZoneId\nimport java.time.ZoneOffset\n\nfun main() {\n    // Current time in New York\n    val newYork = ZonedDateTime.now(ZoneId.of("America/New_York"))\n    println("New York: $newYork")\n    \n    // Conversion from UTC\n    val utcNow = ZonedDateTime.now(ZoneOffset.UTC)\n    val moscowTime = utcNow.withZoneSameInstant(ZoneId.of("Europe/Moscow"))\n    println("Moscow: $moscowTime")\n    \n    // OffsetDateTime for simple offsets\n    val offsetTime = java.time.OffsetDateTime.now(ZoneOffset.ofHours(3))\n    println("+3 hours: $offsetTime")\n}
\n\n

Important: For the user interface, always use ZonedDateTime to correctly handle daylight saving time transitions. OffsetDateTime is suitable for fixed offsets, for example, when working with logs.

\n\n

3. Difference Between Dates: Period and Duration

\n\n

To calculate the difference in time, Kotlin provides two classes: Period (for dates, in days/months/years) and Duration (for precise intervals in seconds/nanoseconds).

\n\n

Period — Working with Calendar Units

\n
import java.time.LocalDate\nimport java.time.Period\n\nfun main() {\n    val birthDate = LocalDate.of(1990, 5, 15)\n    val today = LocalDate.now()\n    val age = Period.between(birthDate, today)\n    \n    println("Age: ${age.years} years, ${age.months} months, ${age.days} days")\n    \n    // Adding a period to a date\n    val nextMonth = today.plus(Period.ofMonths(1))\n    println("In a month: $nextMonth")\n}
\n\n

Duration — Precise Intervals<

Blogs

Book Recommendations