orNull
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:
year the range is platform-dependent, but at least is enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE
month
1..12day
1..31, the upper bound can be less, depending on the monthhour
0..23minute
0..59second
0..59nanosecond
0..999_999_999
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
}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:
year the range is platform-dependent, but at least is enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE
day
1..31, the upper bound can be less, depending on the monthhour
0..23minute
0..59second
0..59nanosecond
0..999_999_999
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
}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:
year the range is platform-dependent, but at least is enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE
month
1..12day
1..31, the upper bound can be less, depending on the monthhour
0..23minute
0..59second
0..59nanosecond
0..999_999_999
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
}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:
year the range is platform-dependent, but at least is enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE
day
1..31, the upper bound can be less, depending on the monthhour
0..23minute
0..59second
0..59nanosecond
0..999_999_999
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
}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:
year the range is platform-dependent, but at least is enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE
month
1..12day
1..31, the upper bound can be less, depending on the monthhour
0..23minute
0..59second
0..59nanosecond
0..999_999_999
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
}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:
year the range is platform-dependent, but at least is enough to represent dates of all instants between Instant.DISTANT_PAST and Instant.DISTANT_FUTURE
day
1..31, the upper bound can be less, depending on the monthhour
0..23minute
0..59second
0..59nanosecond
0..999_999_999
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
}