Reputation: 253
What will be the value of getDisplayName(false, TimeZone.SHORT)
?
I reckon the value is GMToffset
, for example in Singapore it will be GMT+08:00
.
However, on one device (Google Pixel 2 XL) I am getting SGT
.
Why the difference? Could it be Android Oreo or perhaps the SIM card supplying a network-provided time zone? I also thought that such abbreviations or 3/4 letter ids are deprecated already.
Upvotes: 1
Views: 1915
Reputation: 253
Finally, it took awhile but I figured out why the format of timezone returned differed. Here are my findings:
When the language matches the timezone on the device (e.g. English (Malaysia) and the timezone is set to +08:00 Kuala Lumpur) then the timezone will be returned in a 3/4letterId (e.g. MYT). I have no idea why this happens but somehow I stumbled upon this.
Also the way to constantly get timezone in a offset format will be to use this:
DateFormat date = new SimpleDateFormat("XXX", Locale.getDefault());
offset = date.format(new Date());
Upvotes: 1
Reputation: 86324
The outmoded TimeZone.getDefault()
and its modern equivalent, ZoneId.systemDefault()
both return the time zone setting of the JVM. Unless something special has been done to obtain something else, this will be the same as the device’ time zone setting when the JVM is launched. It may later be changed by your program or by other programs running in the same JVM. On the other hand, if you change the device setting, this will not affect the JVM (until a new JVM is launched).
Most likely your devices have different time zone settings. I would consider the setting Asia/Singapore more correct, even though for practical everyday purposes UTC+08:00 is equivalent. The latter is not a true time zone, and when it comes to historical dates it will not always agree with Singapore time. Asia/Singapore time is also known as Singapore Time, SGT, Singapore Standard Time or SST. The last abbreviation is also shared with Samoa Standard Time, which is a completely different time zone (so avoid those three and four letter abbreviations).
A time zone, like Asia/Singapore, is an area (zone) sharing the same time, like the Republic of Singapore, and encompasses not only the current offset from UTC, but also historic and known future changes of offset. An offset from UTC or GMT, like +08:00, on the other hand, hasn’t got any inherent place or time it’s valid. Its validity in a certain area at a certain time follows only from the time zone. So using an offset as the time zone setting of a device is questionable at best. The old class TimeZone
can be used for representing either a time zone or an offset, which may blur the distinction. In java.time
, the distinction has been made between ZoneId
(a time zone with a name, typically and recommended in the region/city format) and a ZoneOffset
. For pragmatic/practical reasons, a ZoneOffset
can also be used where a ZoneId
is required (made possible through inheritance).
As an aside consider throwing away the long outmoded TimeZone
and friends and using java.time
, the modern Java date and time API, instead. It is so much nicer to work with. The modern class you need is ZoneId
. It too has a fine getDisplayName
method.
java.time
comes built-in.org.threeten.bp
with subpackages.java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Upvotes: 0
Reputation: 78
"Three-letter time zone IDs For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them."
https://developer.android.com/reference/java/util/TimeZone.html
Upvotes: 0