Date Period
A special case of DateTimePeriod that only stores the date components and has all time components equal to zero.
A DatePeriod
is automatically returned from all constructor functions for DateTimePeriod if it turns out that the time components are zero.
DateTimePeriod.parse("P1Y3D") as DatePeriod // 1 year and 3 days
Additionally, DatePeriod has its own constructor, the parse function that will fail if any of the time components are not zero, and DatePeriodIso8601Serializer and DatePeriodComponentSerializer, mirroring those of DateTimePeriod.
DatePeriod
values are used in operations on LocalDates and are returned from operations on LocalDates, but they also can be passed anywhere a DateTimePeriod is expected.
On the JVM, there are DatePeriod.toJavaPeriod()
and java.time.Period.toKotlinDatePeriod()
extension functions to convert between kotlinx.datetime
and java.time
objects used for the same purpose.
Samples
import kotlinx.datetime.*
import kotlin.test.*
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.minutes
fun main() {
//sampleStart
// Parsing and formatting a DatePeriod
val datePeriod1 = DatePeriod(years = 1, days = 3)
val string = datePeriod1.toString()
check(string == "P1Y3D")
val datePeriod2 = DatePeriod.parse(string)
check(datePeriod1 == datePeriod2)
//sampleEnd
}
Constructors
Constructs a new DatePeriod.
Functions
Adds two DatePeriod instances.
Converts this kotlinx.datetime.DatePeriod value to a java.time.Period value.