Is “GMT” an abbreviation in Java TimeZone and if “is it normal to use it?”

According to the JavaDoc for TimeZone ...

ID is an identifier for TimeZone, or an abbreviation such as "PST", a full name, for example, "America / Los_Angeles", or a user identifier, for example, "GMT-8: 00". Note that shorthand support for JDK 1.1.x should only use compatibility and full names.

The important point is ...

an abbreviation such as "PST" and Note that abbreviation support is for JDK 1.1.x compatibility only and full names should be used.

Does this mean that “GMT-0: 00” is OK, but “GMT” should be avoided or “GMT” is not considered an abbreviation?

Like my other question , just trying to make it more specific.

0
source share
2 answers

Just looked at the source code. If I read correctly, getTimeZone (String ID) calls a private method called parseCustomTimeZone, which basically checks to see if the identifier starts with GMT and returns null, otherwise getTimeZone returns to the default time zone GMT + 0. Things like UTC, PST, etc. supported in ZoneInfo, which is the inner class of the sun. You can list all available time zones, as javadoc mentions. Here's the code bit corresponding to this in TimeZone:

public static synchronized TimeZone getTimeZone(String ID) { return getTimeZone(ID, true); } ... private static TimeZone getTimeZone(String ID, boolean fallback) { TimeZone tz = ZoneInfo.getTimeZone(ID); if (tz == null) { tz = parseCustomTimeZone(ID); if (tz == null && fallback) { tz = new ZoneInfo(GMT_ID, 0); } } return tz; } ... private static final TimeZone parseCustomTimeZone(String id) { int length; // Error if the length of id isn't long enough or id doesn't // start with "GMT". if ((length = id.length()) < (GMT_ID_LENGTH + 2) || id.indexOf(GMT_ID) != 0) { return null; } ... 

If you're on Java 8, you almost don't want to use this and instead use the new time API. Otherwise use something like joda time.

+1
source

This means using full names instead of abbreviations,

Java docs

Three-letter time zone identifiers For compatibility with JDK 1.1.x, some other three-letter time zone identifiers (for example, "PST", "CTT", "AST") are also supported. However, their use is deprecated since the abbreviation is often used for several time zones (for example, “CST”, maybe US “Central Standard Time” and “Chinese Standard Time”), and the Java platform can then only recognize one of them.

i.e. Instead of using

 TimeZone.getTimeZone("PST"); 

using

 TimeZone.getTimeZone("America/Los_Angeles"); 

recommended for the above reason.

In addition, getTimeZone(String ID) returns

the specified time zone or GMT zone if this identifier cannot be understood.


Finally,

Using TimeZone.getTimeZone("GMT"); quite acceptable, as it is both ID and Name ie, this is one of the acceptable identifiers in the TimeZone.getAvailableIDs() List.

In addition, if the time zone you want is not represented by one of the supported identifiers, then a custom time zone identifier can be specified to create a TimeZone

 CustomID: GMT Sign Hours : Minutes (or) GMT Sign Hours Minutes (or) GMT Sign Hours 
+1
source

Source: https://habr.com/ru/post/1262874/


All Articles