orNull
Constructs a YearMonth instance from the given year-month components or returns null if a value is out of range.
The month component is 1-based.
The supported ranges of components:
year the range is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.
month
1..12
Use YearMonth(year, month) to throw an exception instead of returning null when the parameters are invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a YearMonth value or returning null if components are out of range
check(YearMonth.orNull(2024, 4) == YearMonth(2024, 4))
check(YearMonth.orNull(2024, 13) == null)
//sampleEnd
}Constructs a YearMonth instance from the given year-month components or returns null if a value is out of range.
The supported ranges of components:
year the range is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.
Use YearMonth(year, month) to throw an exception instead of returning null when the parameters are invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a YearMonth value or returning null if components are out of range
check(YearMonth.orNull(2024, Month.APRIL) == YearMonth(2024, Month.APRIL))
check(YearMonth.orNull(Int.MAX_VALUE, Month.APRIL) == null)
//sampleEnd
}Constructs a YearMonth instance from the given year-month components or returns null if a value is out of range.
The month component is 1-based.
The supported ranges of components:
year the range is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.
month
1..12
Use YearMonth(year, month) to throw an exception instead of returning null when the parameters are invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a YearMonth value or returning null if components are out of range
check(YearMonth.orNull(2024, 4) == YearMonth(2024, 4))
check(YearMonth.orNull(2024, 13) == null)
//sampleEnd
}Constructs a YearMonth instance from the given year-month components or returns null if a value is out of range.
The supported ranges of components:
year the range is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.
Use YearMonth(year, month) to throw an exception instead of returning null when the parameters are invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a YearMonth value or returning null if components are out of range
check(YearMonth.orNull(2024, Month.APRIL) == YearMonth(2024, Month.APRIL))
check(YearMonth.orNull(Int.MAX_VALUE, Month.APRIL) == null)
//sampleEnd
}Constructs a YearMonth instance from the given year-month components or returns null if a value is out of range.
The month component is 1-based.
The supported ranges of components:
year the range is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.
month
1..12
Use YearMonth(year, month) to throw an exception instead of returning null when the parameters are invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a YearMonth value or returning null if components are out of range
check(YearMonth.orNull(2024, 4) == YearMonth(2024, 4))
check(YearMonth.orNull(2024, 13) == null)
//sampleEnd
}Constructs a YearMonth instance from the given year-month components or returns null if a value is out of range.
The supported ranges of components:
year the range is unspecified, but at least is enough to represent year-months of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE in any time zone.
Use YearMonth(year, month) to throw an exception instead of returning null when the parameters are invalid.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlinx.datetime.onDay
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a YearMonth value or returning null if components are out of range
check(YearMonth.orNull(2024, Month.APRIL) == YearMonth(2024, Month.APRIL))
check(YearMonth.orNull(Int.MAX_VALUE, Month.APRIL) == null)
//sampleEnd
}