androidjava-timethreetenbp

Time zone day light saving changes problem


In my country, we no longer observe Daylight Saving Time. We now use Standard Time year-round.

Therefor a lot of devices has run into problem. I used different approaches to fix this but just one of them works correctly.

I am trying to convert a epoch time to local time. For example

val epocTime = 1715244851730 // 12:24:11.730 PM GMT+03:30

When I use SimpleDateFormat, I get wrong result.

java.text.SimpleDateFormat("HH:mm", Locale.getDefault(Locale.Category.FORMAT))
.format(Date(time)) // 13:24

Also when I use java.time.LocalDateTime, I get wrong result

java.time.LocalDateTime.ofInstant(
    java.time.Instant.ofEpochMilli(epocTime),
    java.time.ZoneId.systemDefault()
) // 13:24:11.730

But when I use ThreeTen library, I get correct result

org.threeten.bp.LocalDateTime.ofInstant(
    org.threeten.bp.Instant.ofEpochMilli(epocTime),
    org.threeten.bp.ZoneId.systemDefault(),
) // 12:24:11.730

So the question is: Isn't java time a replacement for ThreeTen library?


Solution

  • Similar to Update Android tzdata files and my answer to Android - how to fix java.time.zone.ZoneRulesException: Unknown time-zone

    Warning: to my understanding the emulator only has the tzdata from when the OS version was released and is NOT updated.

    So for old devices which no longer get tzdata updates you'll need to use a third-party library like joda-time-android which seems to update tzdata with some frequency but be aware that you will need update your app if you want to maintain consistency with the current tzdata set.

    A similar situation exists for the ThreeTenABP / ThreeTen Backport where you'll need to update the library to the most recent version to get tzdata updates.