codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

39 / 68 topics
36Kotlin Standard Library37Collections Functions38Sequences in Kotlin39Date and Time API40Kotlin Serialization41Kotlin Coroutines Library42Kotlin HTML DSL43Kotlin CLI
Tutorials/Kotlin/Date and Time API
🎯Kotlin

Date and Time API

Updated 2026-04-20
3 min read

Date and Time API

Introduction

Kotlin provides a robust set of tools for handling date and time operations, which are essential for any application dealing with temporal data. The java.time package introduced in Java 8 has been integrated into Kotlin, offering a modern and comprehensive API for date-time manipulation. This tutorial will guide you through the key features and best practices of using the Date and Time API in Kotlin.

Key Concepts

Instant

An Instant represents a point on the timeline in nanoseconds from the epoch (January 1, 1970, 00:00:00 UTC). It is immutable and thread-safe.

import java.time.Instant

fun main() {
    val now = Instant.now()
    println("Current instant: $now")
}

LocalDate

A LocalDate represents a date without time-zone in the ISO-8601 calendar system. It is useful for handling dates like birthdays, anniversaries, etc.

import java.time.LocalDate

fun main() {
    val today = LocalDate.now()
    println("Today's date: $today")
}

LocalTime

A LocalTime represents a time without a time-zone in the ISO-8601 calendar system. It is used for handling times like meeting schedules, etc.

import java.time.LocalTime

fun main() {
    val now = LocalTime.now()
    println("Current time: $now")
}

LocalDateTime

A LocalDateTime combines LocalDate and LocalTime. It represents a date-time without a time-zone in the ISO-8601 calendar system.

import java.time.LocalDateTime

fun main() {
    val now = LocalDateTime.now()
    println("Current date and time: $now")
}

ZonedDateTime

A ZonedDateTime represents a date-time with a time-zone in the ISO-8601 calendar system. It is useful for handling date-time data that requires timezone information.

import java.time.ZonedDateTime

fun main() {
    val now = ZonedDateTime.now()
    println("Current date, time, and zone: $now")
}

Duration

A Duration represents a quantity of time in seconds and nanoseconds. It is used for measuring the difference between two instants.

import java.time.Duration
import java.time.Instant

fun main() {
    val start = Instant.now()
    // Simulate some operations
    Thread.sleep(1000)
    val end = Instant.now()
    val duration = Duration.between(start, end)
    println("Duration: $duration")
}

Period

A Period represents a quantity of time in terms of years, months, and days. It is used for measuring the difference between two dates.

import java.time.LocalDate
import java.time.Period

fun main() {
    val start = LocalDate.of(2020, 1, 1)
    val end = LocalDate.now()
    val period = Period.between(start, end)
    println("Period: $period")
}

Formatting and Parsing

DateTimeFormatter

DateTimeFormatter is used to format and parse date-time objects. It provides a flexible way to define custom patterns.

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

fun main() {
    val now = LocalDateTime.now()
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
    val formattedDate = now.format(formatter)
    println("Formatted date: $formattedDate")

    val parsedDate = LocalDateTime.parse(formattedDate, formatter)
    println("Parsed date: $parsedDate")
}

Best Practices

  1. Use Immutable Classes: All classes in the java.time package are immutable, which makes them thread-safe and reduces the risk of bugs.

  2. Avoid Using Calendar and SimpleDateFormat: These older APIs are mutable and not thread-safe. Use java.time classes instead.

  3. Handle Time Zones Properly: Always specify a time zone when dealing with date-time data that requires it. Use ZonedDateTime or OffsetDateTime.

  4. Use Duration for Time Differences: For measuring differences between two instants, use Duration. It provides precise and intuitive methods for working with time intervals.

  5. Use Period for Date Differences: For measuring differences between two dates, use Period. It is designed to work with calendar units like years, months, and days.

Real-World Example

Let's create a simple application that calculates the number of days until a specific date.

import java.time.LocalDate
import java.time.Period

fun main() {
    val targetDate = LocalDate.of(2024, 1, 1)
    val today = LocalDate.now()
    val period = Period.between(today, targetDate)

    println("Days until January 1, 2024: ${period.days}")
}

Conclusion

The Date and Time API in Kotlin provides a powerful and flexible way to handle date-time operations. By leveraging the java.time package, you can write robust and maintainable code that handles temporal data effectively. Always follow best practices to ensure your application is thread-safe and handles time zones correctly.

This tutorial should give you a solid foundation for working with dates and times in Kotlin. As you become more familiar with these APIs, you'll find many opportunities to streamline your code and improve its readability and performance.


PreviousSequences in KotlinNext Kotlin Serialization

Recommended Gear

Sequences in KotlinKotlin Serialization