toInstant

Returns an instant that corresponds to this civil datetime value in the specified timeZone.

Note that the conversion is not always well-defined. There can be the following possible situations:

  • Only one instant has this datetime value in the timeZone. In this case, the conversion is unambiguous.

  • No instant has this datetime value in the timeZone. Such a situation appears when the time zone experiences a transition from a lesser to a greater offset. In this case, the conversion is performed with the lesser (earlier) offset, as if the time gap didn't occur yet.

  • Two possible instants can have these datetime components in the timeZone. In this case, the earlier instant is returned.

See also

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   // Converting a local date-time to an instant in a specific time zone
val zone = TimeZone.of("America/New_York")
val localDateTime = LocalDate(2023, 6, 2).atTime(12, 30)
val instant = localDateTime.toInstant(zone)
check(instant == Instant.parse("2023-06-02T16:30:00Z")) 
   //sampleEnd
}

Returns an instant that corresponds to this civil datetime value that happens at the specified UTC offset.

See also

Samples

import kotlinx.datetime.*
import kotlinx.datetime.format.*
import kotlin.test.*
fun main() { 
   //sampleStart 
   // Converting a local date-time to an instant in a specific offset
val offset = UtcOffset.parse("+01:30")
val localDateTime = LocalDate(2023, 6, 2).atTime(12, 30)
val instant = localDateTime.toInstant(offset)
check(instant == Instant.parse("2023-06-02T11:00:00Z")) 
   //sampleEnd
}