How to convert only the date without time to a string in Kotlin

1 Answer

0 votes
import java.time.LocalDate
import java.time.format.DateTimeFormatter

// Convert a LocalDate to a string (YYYY-MM-DD)
fun dateToString(d: LocalDate): String =
    d.format(DateTimeFormatter.ISO_LOCAL_DATE)

// Build a LocalDate from integers
fun makeDate(y: Int, m: Int, d: Int): LocalDate =
    LocalDate.of(y, m, d)

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

    // 2. Hard-coded date
    val myDate = makeDate(2025, 12, 7)
    println("Hard-coded date is: ${dateToString(myDate)}")
}



/*
run:

Today's date is: 2026-05-31
Hard-coded date is: 2025-12-07

*/

 



answered 3 hours ago by avibootz

Related questions

...