Skip to content
The App Code
How-to

Format a Date in Kotlin

Format date and time values using the java.time API and DateTimeFormatter.

Also known as: Kotlin DateTimeFormatter, format LocalDateTime Kotlin

beginner

Use the immutable java.time types with a DateTimeFormatter to produce fixed or localized strings. DateTimeFormatter is thread-safe and reusable.

What it is

Kotlin uses Java's java.time package (JSR-310) for dates. Unlike the old SimpleDateFormat, DateTimeFormatter is immutable and thread-safe, so you can share a single formatter across threads. Use ofPattern for a fixed pattern or ofLocalizedDateTime for locale-aware output.

Work with the type that matches your data: LocalDate for a calendar date, LocalDateTime for date-plus-time without a zone, and ZonedDateTime/Instant when a time zone matters. On Android, enable core library desugaring to use java.time below API 26.

Worked example

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

fun main() {
    val now = LocalDateTime.of(2026, 7, 2, 15, 14)
    val fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")
    println(now.format(fmt)) // 2026-07-02 15:14
}

Failure mode — when it misleads

The old SimpleDateFormat is not thread-safe and sharing one instance across threads causes silent, hard-to-reproduce corruption — prefer DateTimeFormatter. Also watch pattern letters: MM is month but mm is minutes, and DD (day-of-year) is a common mistake for dd (day-of-month).

Sources & further reading