parseIsoString

Common
JVM
JS
Native
1.0
fun parseIsoString(value: String): Duration
(source)

Parses a string that represents a duration in ISO-8601 format and returns the parsed Duration value.

import kotlin.test.*

import kotlin.time.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.nanoseconds
import kotlin.time.Duration.Companion.seconds

fun main(args: Array<String>) {
//sampleStart
val isoFormatString = "PT1H30M"
val defaultFormatString = "1h 30m"

println(Duration.parseIsoString(isoFormatString)) // 1h 30m
// Duration.parseIsoString(defaultFormatString) //  will fail
println(Duration.parseIsoStringOrNull(defaultFormatString)) // null
//sampleEnd
}

Exceptions

IllegalArgumentException - if the string doesn't represent a duration in ISO-8601 format.