setDateTime

fun setDateTime(localDateTime: LocalDateTime)(source)

Writes the contents of the specified localDateTime to this DateTimeComponents. The localDateTime is written to the year, monthNumber, dayOfMonth, dayOfWeek, hour, hourOfAmPm, amPm, minute, second and nanosecond fields.

If any of the fields are already set, they will be overwritten.

Samples

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

fun main() { 
   //sampleStart 
   // Setting the date-time components for formatting
val dateTime = LocalDate(2021, 3, 28).atTime(2, 16, 20)
val customFormat = DateTimeComponents.Format {
    dateTime(LocalDateTime.Formats.ISO)
    char('[')
    timeZoneId()
    char(']')
}
val formatted = customFormat.format {
    setDateTime(dateTime)
    timeZoneId = "America/New_York"
}
check(formatted == "2021-03-28T02:16:20[America/New_York]") 
   //sampleEnd
}