parse
Parses the given input string as T using this format.
Throws
if the input string is not in the expected format or the value is invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Parsing a string that is expected to be in the given format
check(LocalDate.Formats.ISO.parse("2021-02-07") == LocalDate(2021, 2, 7))
try {
LocalDate.Formats.ISO.parse("2021-02-07T")
fail("Expected IllegalArgumentException")
} catch (e: IllegalArgumentException) {
// the input string is not in the expected format
}
try {
LocalDate.Formats.ISO.parse("2021-02-40")
fail("Expected IllegalArgumentException")
} catch (e: IllegalArgumentException) {
// the input string is in the expected format, but the value is invalid
}
// to parse strings that have valid formats but invalid values, use `DateTimeComponents`:
check(DateTimeComponents.Format { date(LocalDate.Formats.ISO) }.parse("2021-02-40").dayOfMonth == 40)
//sampleEnd
}