atTime

fun LocalDate.atTime(hour: Int, minute: Int, second: Int = 0, nanosecond: Int = 0): LocalDateTime(source)

Combines this date's components with the specified time components into a LocalDateTime value.

For finding an instant that corresponds to the start of a date in a particular time zone, consider using LocalDate.atStartOfDayIn function because a day does not always start at the fixed time 0:00:00.

Pitfall: since LocalDateTime is not tied to a particular time zone, the resulting LocalDateTime may not exist in the implicit time zone. For example, LocalDate(2021, 3, 28).atTime(2, 16, 20) will successfully create a LocalDateTime, even though in Berlin, times between 2:00 and 3:00 do not exist on March 28, 2021 due to the transition to DST.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value from a LocalDate and a LocalTime
val date = LocalDate(2024, Month.APRIL, 16)
val dateTime = date.atTime(13, 30)
check(dateTime == LocalDateTime(2024, Month.APRIL, 16, 13, 30)) 
   //sampleEnd
}

Combines this date's components with the specified LocalTime components into a LocalDateTime value.

For finding an instant that corresponds to the start of a date in a particular time zone consider using LocalDate.atStartOfDayIn function because a day does not always start at the fixed time 0:00:00.

Pitfall: since LocalDateTime is not tied to a particular time zone, the resulting LocalDateTime may not exist in the implicit time zone. For example, LocalDate(2021, 3, 28).atTime(LocalTime(2, 16, 20)) will successfully create a LocalDateTime, even though in Berlin, times between 2:00 and 3:00 do not exist on March 28, 2021, due to the transition to DST.

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.random.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   // Constructing a LocalDateTime value from a LocalDate and a LocalTime
val date = LocalDate(2024, Month.APRIL, 16)
val time = LocalTime(13, 30)
val dateTime = date.atTime(time)
check(dateTime == LocalDateTime(2024, Month.APRIL, 16, 13, 30)) 
   //sampleEnd
}