Year Month Progression
A progression of values of type YearMonth.
Samples
import kotlinx.datetime.*
import kotlin.random.Random
import kotlin.test.Test
fun main() {
//sampleStart
// Creating YearMonthProgression with a step size other than 1 day
check(
(YearMonth(2000, 1)..YearMonth(2000, 5)).step(2, DateTimeUnit.MONTH).toList() == listOf(
YearMonth(2000, 1),
YearMonth(2000, 3),
YearMonth(2000, 5)
))
check(
(YearMonth(2000, 5) downTo YearMonth(2000, 1)).step(2, DateTimeUnit.MONTH).toList() == listOf(
YearMonth(2000, 5),
YearMonth(2000, 3),
YearMonth(2000, 1)
))
//sampleEnd
}
import kotlinx.datetime.*
import kotlin.random.Random
import kotlin.test.Test
fun main() {
//sampleStart
// Creating YearMonthProgression and flipping its direction
check(
(YearMonth(2000, 1)..YearMonth(2000, 5)).reversed().toList() == listOf(
YearMonth(2000, 5),
YearMonth(2000, 4),
YearMonth(2000, 3),
YearMonth(2000, 2),
YearMonth(2000, 1)
))
check(
(YearMonth(2000, 5) downTo YearMonth(2000, 1)).reversed().toList() == listOf(
YearMonth(2000, 1),
YearMonth(2000, 2),
YearMonth(2000, 3),
YearMonth(2000, 4),
YearMonth(2000, 5)
))
//sampleEnd
}
import kotlinx.datetime.*
import kotlin.random.Random
import kotlin.test.Test
fun main() {
//sampleStart
// Getting the first and last elements of a YearMonthProgression
check((YearMonth(2000, 1)..YearMonth(2000, 5)).first() == YearMonth(2000, 1))
check((YearMonth(2000, 1)..YearMonth(2000, 5)).last() == YearMonth(2000, 5))
check((YearMonth(2000, 1)..YearMonth(2000, 6)).step(2, DateTimeUnit.MONTH).first() == YearMonth(2000, 1))
check((YearMonth(2000, 1)..YearMonth(2000, 6)).step(2, DateTimeUnit.MONTH).last() == YearMonth(2000, 5))
check((YearMonth(2000, 5)..YearMonth(2000, 1)).firstOrNull() == null)
check((YearMonth(2000, 5)..YearMonth(2000, 1)).lastOrNull() == null)
//sampleEnd
}
Inheritors
Properties
Returns the number of months in the progression. Returns Int.MAX_VALUE if the number of months overflows Int
Functions
Returns true iff every element in elements is a member of the progression.
Returns the first YearMonth of the YearMonthProgression.
Returns the first YearMonth of the YearMonthProgression, or null if the progression is empty.
Returns the last YearMonth of the YearMonthProgression.
Returns the last YearMonth of the YearMonthProgression, or null if the progression is empty.
Returns a random YearMonth within the bounds of the YearMonthProgression.
Returns a random YearMonth within the bounds of the YearMonthProgression or null if the progression is empty.
Returns a reversed YearMonthProgression, i.e. one that goes from last to first. The sign of the step is switched, in order to reverse the direction of the progression.
Returns a YearMonthProgression with the same start and end, but a changed step value.