parse

expect fun parse(input: CharSequence, format: DateTimeFormat<LocalTime> = getIsoTimeFormat()): LocalTime(source)

A shortcut for calling DateTimeFormat.parse.

Parses a string that represents time-of-day and returns the parsed LocalTime value.

If format is not specified, Formats.ISO is used. 23:40:57.120 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 LocalTime are exceeded.

Samples

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

fun main() { 
   //sampleStart 
   // Parsing a LocalTime from a string using predefined and custom formats
check(LocalTime.parse("08:30:15.123456789") == LocalTime(8, 30, 15, 123_456_789))
check(LocalTime.parse("08:30:15") == LocalTime(8, 30, 15))
check(LocalTime.parse("08:30") == LocalTime(8, 30))
val customFormat = LocalTime.Format {
    hour(); char(':'); minute(); char(':'); second()
    alternativeParsing({ char(',') }) { char('.') } // parse either a dot or a comma
    secondFraction(fixedLength = 3)
}
check(LocalTime.parse("08:30:15,123", customFormat) == LocalTime(8, 30, 15, 123_000_000)) 
   //sampleEnd
}