Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8311960

Introduce method in TimeZone to provide modern identifier name

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Withdrawn
    • Icon: P4 P4
    • tbd
    • core-libs
    • None
    • behavioral
    • minimal
    • Introduce a static method to TimeZone class. This method simply queries an unmodifiable map from an internal implementation of TimeZone.
    • Java API
    • SE

      Summary

      Introduce TimeZone.getModernIdentifier(String ID) which allows users to enforce the usage of modern (tzdb) identifiers.

      Problem

      Currently in java.util.TimeZone a user can call TimeZone.getAvailableIDs() which returns 600+ identifiers. Although all the results are technically valid, the results contain the deprecated 3-letter Java identifiers, as well as historical TZDB identifiers that exist for compatibility reasons, which are ultimately mapped to a modern identifier.

      For a user not concerned with compatibility issues, they should be encouraged to use modern identifiers, but there currently exists no API to distinguish which ID is "modern" or "historical".

      Users have noticed the different ID formats, (for example, JDK-6214500) and have came up with hacks such as, "I managed to deduce by inspection that the up-to-date time zones are the ones with a '/' character in their ID. They are all of the form '/' (or 'Etc/')". Such a hack is actually incorrect, as although it would exclude the deprecated 3-letter JDK IDs, it would exclude "modern" TZDB identifiers that do not follow that format such as "MST7MDT", "PST8PDT", etc. In addition, this hack would also exclude "historical" (not deprecated) TZDB identifiers, which may not be ideal.

      Solution

      Introduce the static method TimeZone.getModernIdentifier(String ID) which ensures users are using a modern identifier. This method will return the modern equivalent that ID maps to, if one exists, otherwise it will return the input ID.

      For example, if a user calls TimeZone.getAvailableIDs() and picks out an ID, they can subsequently call TimeZone.getModernIdentifier(String ID) to ensure they are working with a "modern" TZDB ID.

      Specification

      --- a/src/java.base/share/classes/java/util/TimeZone.java
      +++ b/src/java.base/share/classes/java/util/TimeZone.java
      /**
       * {@return the modern ID that {@code ID} maps to, if one exists, otherwise
       * {@code ID} is returned}
       *
       * The JDK and the TZDB maintain historical identifiers for compatibility
       * reasons that ultimately map to modern identifiers. This method can be used
       * to ensure a modern identifier is used. For example, {@code
       * TimeZone.getModernMapping("Japan")} returns {@code "Asia/Tokyo"}. In
       * contrast, {@code TimeZone.getModernMapping("Asia/Tokyo")} returns {@code "Asia/Tokyo"}.
       *
       * @param ID the ID for a TimeZone.
       * @throws NullPointerException This method throws a
       * {@code NullPointerException} if {@code ID} is {@code null}
       * @since 22
       */
      public static String getModernIdentifier(String ID) {
          Objects.requireNonNull(ID, "ID must not be null");
          return ZoneInfo.getAliasTable().getOrDefault(ID, ID);
      }

            jlu Justin Lu
            jlu Justin Lu
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: