https://kotlinlang.org logo
#stdlib
Title
# stdlib
u

윤동환

11/01/2023, 12:51 AM
How can i convert two java.time.LocalDateTime instances to kotlin.time.Duration? i can’t find stdlib converting functions like java.time.Duration.between(t1, t2) for represent two time’s diffrences.
k

kevin.cianfarini

11/01/2023, 12:55 AM
That's because the difference is between two instants in time, not two
LocalDateTime
instances. Any conversion on local date time is ambiguous because of timezones and daylight savings time. You must instead do this conversion on
Instant
.
Copy code
val tz = TimeZone.UTC
val dt1 = LocalDateTime(...).toInstant(tz)
val dt2 = LocalDateTime(...).toInstant(tz)

val duration = dt1 - dt2
👍 1
u

윤동환

11/01/2023, 1:00 AM
ah thanks! reveal TimeZone is important things! thank you