orNull

actual fun orNull(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int): LocalDateTime?(source)

Constructs a LocalDateTime instance from the given date and time components or returns null if a value is out of range.

The components month and day are 1-based.

The supported ranges of components:

Use LocalDateTime(year, month, day, hour, minute, second, nanosecond) to throw an exception instead of returning null when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using `orNull`
val dateTime = LocalDateTime.orNull(2024, 2, 15, 16, 48, 59, 999_999_999)
// For valid values, `orNull` is equivalent to the constructor
check(dateTime == LocalDateTime(2024, 2, 15, 16, 48, 59, 999_999_999))
// If a value can not be constructed, null is returned
check(LocalDateTime.orNull(2024, 2, 31, 16, 48) == null) // Invalid day
check(LocalDateTime.orNull(2024, 2, 15, 24, 48) == null) // Invalid hour
check(LocalDateTime.orNull(2024, 2, 15, 16, 60) == null) // Invalid minute
check(LocalDateTime.orNull(2024, 2, 15, 16, 48, 60) == null) // Invalid second
check(LocalDateTime.orNull(2024, 2, 15, 16, 48, 59, 1_000_000_000) == null) // Invalid nanosecond 
   //sampleEnd
}

actual fun orNull(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int): LocalDateTime?(source)

Constructs a LocalDateTime instance from the given date and time components or returns null if a value is out of range.

The supported ranges of components:

Use LocalDateTime(year, month, day, hour, minute, second, nanosecond) to throw an exception instead of returning null when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using `orNull` with Month enum
val dateTime = LocalDateTime.orNull(2024, Month.FEBRUARY, 15, 16, 48, 59, 999_999_999)
// For valid values, `orNull` is equivalent to the constructor
check(dateTime == LocalDateTime(2024, Month.FEBRUARY, 15, 16, 48, 59, 999_999_999))
// If a value can not be constructed, null is returned
check(LocalDateTime.orNull(2024, Month.FEBRUARY, 31, 16, 48) == null) // Invalid day
check(LocalDateTime.orNull(2024, Month.FEBRUARY, 15, 24, 48) == null) // Invalid hour 
   //sampleEnd
}
expect fun orNull(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime?(source)

Constructs a LocalDateTime instance from the given date and time components or returns null if a value is out of range.

The components month and day are 1-based.

The supported ranges of components:

Use LocalDateTime(year, month, day, hour, minute, second, nanosecond) to throw an exception instead of returning null when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using `orNull`
val dateTime = LocalDateTime.orNull(2024, 2, 15, 16, 48, 59, 999_999_999)
// For valid values, `orNull` is equivalent to the constructor
check(dateTime == LocalDateTime(2024, 2, 15, 16, 48, 59, 999_999_999))
// If a value can not be constructed, null is returned
check(LocalDateTime.orNull(2024, 2, 31, 16, 48) == null) // Invalid day
check(LocalDateTime.orNull(2024, 2, 15, 24, 48) == null) // Invalid hour
check(LocalDateTime.orNull(2024, 2, 15, 16, 60) == null) // Invalid minute
check(LocalDateTime.orNull(2024, 2, 15, 16, 48, 60) == null) // Invalid second
check(LocalDateTime.orNull(2024, 2, 15, 16, 48, 59, 1_000_000_000) == null) // Invalid nanosecond 
   //sampleEnd
}

expect fun orNull(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime?(source)

Constructs a LocalDateTime instance from the given date and time components or returns null if a value is out of range.

The supported ranges of components:

Use LocalDateTime(year, month, day, hour, minute, second, nanosecond) to throw an exception instead of returning null when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using `orNull` with Month enum
val dateTime = LocalDateTime.orNull(2024, Month.FEBRUARY, 15, 16, 48, 59, 999_999_999)
// For valid values, `orNull` is equivalent to the constructor
check(dateTime == LocalDateTime(2024, Month.FEBRUARY, 15, 16, 48, 59, 999_999_999))
// If a value can not be constructed, null is returned
check(LocalDateTime.orNull(2024, Month.FEBRUARY, 31, 16, 48) == null) // Invalid day
check(LocalDateTime.orNull(2024, Month.FEBRUARY, 15, 24, 48) == null) // Invalid hour 
   //sampleEnd
}
actual fun orNull(year: Int, month: Int, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int): LocalDateTime?(source)

Constructs a LocalDateTime instance from the given date and time components or returns null if a value is out of range.

The components month and day are 1-based.

The supported ranges of components:

Use LocalDateTime(year, month, day, hour, minute, second, nanosecond) to throw an exception instead of returning null when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using `orNull`
val dateTime = LocalDateTime.orNull(2024, 2, 15, 16, 48, 59, 999_999_999)
// For valid values, `orNull` is equivalent to the constructor
check(dateTime == LocalDateTime(2024, 2, 15, 16, 48, 59, 999_999_999))
// If a value can not be constructed, null is returned
check(LocalDateTime.orNull(2024, 2, 31, 16, 48) == null) // Invalid day
check(LocalDateTime.orNull(2024, 2, 15, 24, 48) == null) // Invalid hour
check(LocalDateTime.orNull(2024, 2, 15, 16, 60) == null) // Invalid minute
check(LocalDateTime.orNull(2024, 2, 15, 16, 48, 60) == null) // Invalid second
check(LocalDateTime.orNull(2024, 2, 15, 16, 48, 59, 1_000_000_000) == null) // Invalid nanosecond 
   //sampleEnd
}

actual fun orNull(year: Int, month: Month, day: Int, hour: Int, minute: Int, second: Int, nanosecond: Int): LocalDateTime?(source)

Constructs a LocalDateTime instance from the given date and time components or returns null if a value is out of range.

The supported ranges of components:

Use LocalDateTime(year, month, day, hour, minute, second, nanosecond) to throw an exception instead of returning null when the parameters are invalid.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value using `orNull` with Month enum
val dateTime = LocalDateTime.orNull(2024, Month.FEBRUARY, 15, 16, 48, 59, 999_999_999)
// For valid values, `orNull` is equivalent to the constructor
check(dateTime == LocalDateTime(2024, Month.FEBRUARY, 15, 16, 48, 59, 999_999_999))
// If a value can not be constructed, null is returned
check(LocalDateTime.orNull(2024, Month.FEBRUARY, 31, 16, 48) == null) // Invalid day
check(LocalDateTime.orNull(2024, Month.FEBRUARY, 15, 24, 48) == null) // Invalid hour 
   //sampleEnd
}