How to format a date using different formats in Kotlin

1 Answer

0 votes
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.Date
import java.text.SimpleDateFormat

fun main() {

    // Modern Kotlin/Java date-time API
    val now: LocalDateTime = LocalDateTime.now()

    println("Original LocalDateTime: $now\n")

    // --- BASIC COMPONENTS (java.time) ---

    println("Year (4 digits): " + now.format(DateTimeFormatter.ofPattern("yyyy")))
    println("Year (2 digits): " + now.format(DateTimeFormatter.ofPattern("yy")))

    println("Month (01-12): " + now.format(DateTimeFormatter.ofPattern("MM")))
    println("Month name (short): " + now.format(DateTimeFormatter.ofPattern("MMM")))
    println("Month name (full): " + now.format(DateTimeFormatter.ofPattern("MMMM")))

    println("Day of month: " + now.format(DateTimeFormatter.ofPattern("dd")))
    println("Day of week (short): " + now.format(DateTimeFormatter.ofPattern("E")))
    println("Day of week (full): " + now.format(DateTimeFormatter.ofPattern("EEEE")))

    // --- COMMON FULL FORMATS ---

    println("YYYY-MM-DD: " + now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")))
    println("DD/MM/YYYY: " + now.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")))
    println("MM-DD-YYYY: " + now.format(DateTimeFormatter.ofPattern("MM-dd-yyyy")))

    println(
        "YYYY/MM/DD HH:MM:SS: " +
        now.format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))
    )

    // --- TIME FORMATS ---

    println("24-hour time: " + now.format(DateTimeFormatter.ofPattern("HH:mm:ss")))
    println("12-hour time: " + now.format(DateTimeFormatter.ofPattern("hh:mm:ss a")))

    // --- HUMAN-READABLE TEXT FORMATS ---

    println(
        "Full readable date: " +
        now.format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy"))
    )

    println(
        "Full date + time: " +
        now.format(DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy HH:mm:ss"))
    )

    // --- ISO / RFC STANDARDS ---

    println("ISO_LOCAL_DATE: " + now.format(DateTimeFormatter.ISO_LOCAL_DATE))
    println("ISO_LOCAL_DATE_TIME: " + now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME))
    println(
        "ISO_ZONED_DATE_TIME: " +
        now.atZone(ZoneId.systemDefault()).format(DateTimeFormatter.ISO_ZONED_DATE_TIME)
    )

    // --- LEGACY SimpleDateFormat (still used in older code) ---

    val legacyDate: Date = Date()
    val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")

    println("\nLegacy Date (SimpleDateFormat): " + sdf.format(legacyDate))
}



/*
run:

Original LocalDateTime: 2026-05-20T16:38:37.784805646

Year (4 digits): 2026
Year (2 digits): 26
Month (01-12): 05
Month name (short): May
Month name (full): May
Day of month: 20
Day of week (short): Wed
Day of week (full): Wednesday
YYYY-MM-DD: 2026-05-20
DD/MM/YYYY: 20/05/2026
MM-DD-YYYY: 05-20-2026
YYYY/MM/DD HH:MM:SS: 2026/05/20 16:38:37
24-hour time: 16:38:37
12-hour time: 04:38:37 PM
Full readable date: Wednesday, May 20, 2026
Full date + time: Wednesday, May 20, 2026 16:38:37
ISO_LOCAL_DATE: 2026-05-20
ISO_LOCAL_DATE_TIME: 2026-05-20T16:38:37.784805646
ISO_ZONED_DATE_TIME: 2026-05-20T16:38:37.784805646Z[Etc/UTC]

Legacy Date (SimpleDateFormat): 2026-05-20 16:38:37

*/

 



answered May 20 by avibootz
...