0

According to the JavaDoc for TimeZone...

ID - the ID for a TimeZone, either an abbreviation such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-8:00". Note that the support of abbreviations is for JDK 1.1.x compatibility only and full names should be used.

The important point being...

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

Does that mean "GMT-0:00" is ok but "GMT" should be avoided or is "GMT" not considered an abbreviation?

Similar to my other question just trying to make it more specific.

Community
  • 1
  • 1
Jackie
  • 21,969
  • 32
  • 147
  • 289

2 Answers2

1

Just looked at the source code. If I read it correctly, getTimeZone(String ID) calls a private method called parseCustomTimeZone, which basically checks if the id starts with GMT and returns null otherwise in which case getTimeZone falls back to a default timezone GMT+ 0. Things like UTC, PST, etc. are supported in ZoneInfo, which is sun internal class. You can list all available timezones as the javadoc mentions. Here's the bits of code relevant for 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 are on Java 8, you pretty much want to not use that and instead use the new time API. Otherwise, use something like joda time.

Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
  • So to be clear things like PST are handled by ZoneInfo which should be avoided right? Also, GMT is not part of that so it is ok. Does that sound right? – Jackie Sep 15 '14 at 14:52
1

It means Use full names instead of Abbreviation,

Java Docs

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.

i.e. Instead of using

TimeZone.getTimeZone("PST");

use

TimeZone.getTimeZone("America/Los_Angeles");

is recommended because of above mentioned reason.

Also, getTimeZone(String ID) returns

the specified Time Zone, or the GMT zone if the given ID cannot be understood.


In conclusion,

Using TimeZone.getTimeZone("GMT"); is perfectly acceptable , since it is both ID and Name i.e it is one of the acceptable ID's in TimeZone.getAvailableIDs() List.

Also, If the time zone you want is not represented by one of the supported IDs, then a custom time zone ID can be specified to produce a TimeZone

CustomID:
         GMT Sign Hours : Minutes (or)
         GMT Sign Hours Minutes (or)
         GMT Sign Hours
Ravi Yenugu
  • 3,895
  • 5
  • 40
  • 58
  • 2
    As far as I understand, `TimeZone.getAvailableIDs()` returns all supported IDs, including deprecated abbreviations. It does return `PST`, for example. – shmosel Jan 16 '17 at 09:24