orNull

actual fun orNull(year: Int, month: Int): YearMonth?(source)

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:

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
}

actual fun orNull(year: Int, month: Month): YearMonth?(source)

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:

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
}
expect fun orNull(year: Int, month: Int): YearMonth?(source)

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:

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
}

expect fun orNull(year: Int, month: Month): YearMonth?(source)

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:

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
}
actual fun orNull(year: Int, month: Int): YearMonth?(source)

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:

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
}

actual fun orNull(year: Int, month: Month): YearMonth?(source)

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:

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
}