parseOrNull
Parses the ISO 8601 duration representation as a DatePeriod, for example, P1Y2M30D, or returns null if the string does not represent a valid DatePeriod.
This function is equivalent to DateTimePeriod.parse, but will fail if any of the time components are not zero.
See also
for a version of this function that throws on incorrect input
Samples
import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
fun main() {
//sampleStart
// Parsing a string representation of a DatePeriod or failing
with(DatePeriod.parseOrNull("P1Y16M60D")) {
check(this != null)
check(this == DatePeriod(years = 2, months = 4, days = 60))
}
with(DatePeriod.parseOrNull("P1Y2M3DT1H-60M")) {
check(this != null)
check(this == DatePeriod(years = 1, months = 2, days = 3))
}
check(DatePeriod.parseOrNull("this is not a period") == null)
check(DatePeriod.parseOrNull("P9999999999999999999999999999M") == null)
//sampleEnd
}Content copied to clipboard