DatePeriod

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

Link copied to clipboard
constructor(years: Int = 0, months: Int = 0, days: Int = 0)

Constructs a new DatePeriod.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val days: Int

The number of calendar days. Can be negative.

Link copied to clipboard
open override val hours: Int

The number of whole hours in this period. Always equal to zero.

Link copied to clipboard
open override val minutes: Int

The number of whole minutes in this period. Always equal to zero.

Link copied to clipboard
open override val nanoseconds: Int

The number of nanoseconds in this period. Always equal to zero.

Link copied to clipboard
open override val seconds: Int

The number of whole seconds in this period. Always equal to zero.

Functions

Link copied to clipboard
operator fun DatePeriod.plus(other: DatePeriod): DatePeriod

Adds two DatePeriod instances.

Link copied to clipboard