Local Date Time
Constructs a LocalDateTime instance from the given date and time components.
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..12
day
1..31
, the upper bound can be less, depending on the monthhour
0..23
minute
0..59
second
0..59
nanosecond
0..999_999_999
Throws
if any parameter is out of range or if day is invalid for the given monthNumber and year.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
year = 2024,
month = 2,
day = 15,
hour = 16,
minute = 48,
second = 59,
nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, 2, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
year = 2024,
month = 2,
day = 15,
hour = 16,
minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, 2, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48))
//sampleEnd
}
Constructs a LocalDateTime instance from the given date and time components.
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 all values of the Month enum
day
1..31
, the upper bound can be less, depending on the monthhour
0..23
minute
0..59
second
0..59
nanosecond
0..999_999_999
Throws
if any parameter is out of range, or if day is invalid for the given month and year.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Constructing a LocalDateTime value using its constructor
val dateTime = LocalDateTime(
year = 2024,
month = Month.FEBRUARY,
day = 15,
hour = 16,
minute = 48,
second = 59,
nanosecond = 999_999_999
)
check(dateTime.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTime.time == LocalTime(16, 48, 59, 999_999_999))
val dateTimeWithoutSeconds = LocalDateTime(
year = 2024,
month = Month.FEBRUARY,
day = 15,
hour = 16,
minute = 48
)
check(dateTimeWithoutSeconds.date == LocalDate(2024, Month.FEBRUARY, 15))
check(dateTimeWithoutSeconds.time == LocalTime(16, 48))
//sampleEnd
}
Constructs a LocalDateTime instance by combining the given date and time parts.
Samples
import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() {
//sampleStart
// Converting a LocalDate and a LocalTime to a LocalDateTime value and getting them back
val date = LocalDate(2024, 2, 15)
val time = LocalTime(16, 48)
val dateTime = LocalDateTime(date, time)
check(dateTime.date == date)
check(dateTime.time == time)
check(dateTime == date.atTime(time))
check(dateTime == time.atDate(date))
//sampleEnd
}