Get Current TimeZone Abbrevation Android

Viewed 87

Is there any library that I can get the current timezone abbrevation for Android like in this list: https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

For example, I am in Turkey and the timezone abbrevation for Turkey is TRT and I want to display it to user? Here is the code I used so far:

fun getTimeZoneAbbrevation():String {
val id = TimeZone.getDefault().id
return ZoneId.of(id).getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH)  }

And the result I get from this function is "Europe/Istanbul", what I except is TRT.

1 Answers

This should work:

    val zone = ZonedDateTime.now(ZoneId.systemDefault()).zone
    println(zone.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH))

When I input Europe/Istanbul I get TRT:

    val zone = ZoneId.of("Europe/Istanbul")
    println(zone.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH))

enter image description here

It would be extremely strange if the code wouldn't work on your system.

I have changed my default time zone to Istanbul and I am getting TRT when running the first block of code:

enter image description here

Related