parse

expect fun parse(input: CharSequence, format: DateTimeFormat<LocalDate> = getIsoDateFormat()): LocalDate(source)

A shortcut for calling DateTimeFormat.parse.

Parses a string that represents a date and returns the parsed LocalDate value.

If format is not specified, Formats.ISO is used. 2023-01-02 is an example of a string in this format.

See also

for formatting using the default format.

for formatting using a custom format.

Throws

if the text cannot be parsed or the boundaries of LocalDate are exceeded.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Parsing LocalDate values using predefined and custom formats
check(LocalDate.parse("2024-04-16") == LocalDate(2024, Month.APRIL, 16))
val customFormat = LocalDate.Format {
    monthName(MonthNames.ENGLISH_ABBREVIATED); char(' '); dayOfMonth(); chars(", "); year()
}
check(LocalDate.parse("Apr 16, 2024", customFormat) == LocalDate(2024, Month.APRIL, 16)) 
   //sampleEnd
}