set Time
Writes the contents of the specified localTime to this DateTimeComponents. The localTime is written to the hour, hourOfAmPm, amPm, minute, second and nanosecond fields.
If any of the fields are already set, they will be overwritten.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Formatting and parsing a time in complex scenarios
val format = DateTimeComponents.Format {
hour(); char(':'); minute(); char(':'); second(); char('.'); secondFraction(1, 9)
}
val formattedTime = format.format {
setTime(LocalTime(3, 46, 58, 123_456_789))
check(hour == 3)
check(minute == 46)
check(second == 58)
check(nanosecond == 123_456_789)
check(hourOfAmPm == 3)
check(amPm == AmPmMarker.AM)
}
check(formattedTime == "03:46:58.123456789")
val parsedTime = format.parse("03:46:58.123456789")
check(parsedTime.toLocalTime() == LocalTime(3, 46, 58, 123_456_789))
check(parsedTime.hour == 3)
check(parsedTime.minute == 46)
check(parsedTime.second == 58)
check(parsedTime.nanosecond == 123_456_789)
check(parsedTime.hourOfAmPm == null)
check(parsedTime.amPm == null)
//sampleEnd
}