parseIsoString

Parses a string that represents a duration in a restricted ISO-8601 composite representation and returns the parsed Duration value. Composite representation is a relaxed version of ISO-8601 duration format that supports negative durations and negative values of individual components.

The following restrictions are imposed:

  • The only allowed non-time designator is days (D). Y (years), W (weeks), and M (months) are not supported.

  • Day is considered to be exactly 24 hours (24-hour clock time scale).

  • Alternative week-based representation ["P"][number]["W"] is not supported.

Since Kotlin

1.2

Samples



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() { 
   //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
}

Throws

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