setDateTimeOffset

fun setDateTimeOffset(instant: Instant, utcOffset: UtcOffset)(source)

Writes the contents of the specified instant to this DateTimeComponents.

This method is almost always equivalent to the following code:

setDateTime(instant.toLocalDateTime(offset))
setOffset(utcOffset)

However, this also works for instants that are too large to be represented as a LocalDateTime.

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 Instant and UTC offset components for formatting
val instant = Instant.parse("2021-03-28T02:16:20+03:00")
val offset = UtcOffset(3, 0)
val formatted = DateTimeComponents.Formats.RFC_1123.format {
    setDateTimeOffset(instant, offset)
}
check(formatted == "Sun, 28 Mar 2021 02:16:20 +0300") 
   //sampleEnd
}

fun setDateTimeOffset(localDateTime: LocalDateTime, utcOffset: UtcOffset)(source)

Writes the contents of the specified localDateTime and utcOffset to this DateTimeComponents.

A shortcut for calling setDateTime and setOffset separately.

If localDateTime is obtained from an Instant using LocalDateTime.toInstant, it is recommended to use setDateTimeOffset that accepts an Instant directly.

Samples

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

fun main() { 
   //sampleStart 
   // Setting the date-time and UTC offset components for parsing
val localDateTime = LocalDate(2021, 3, 28).atTime(2, 16, 20)
val offset = UtcOffset(3, 0)
val formatted = DateTimeComponents.Formats.RFC_1123.format {
    setDateTimeOffset(localDateTime, offset)
}
check(formatted == "Sun, 28 Mar 2021 02:16:20 +0300") 
   //sampleEnd
}