src/share/classes/java/util/TimeZone.java

Print this page




 542      */
 543     public static TimeZone getTimeZone(ZoneId zoneId) {
 544         String tzid = zoneId.getId(); // throws an NPE if null
 545         char c = tzid.charAt(0);
 546         if (c == '+' || c == '-') {
 547             tzid = "GMT" + tzid;
 548         } else if (c == 'Z' && tzid.length() == 1) {
 549             tzid = "UTC";
 550         }
 551         return getTimeZone(tzid, true);
 552     }
 553 
 554     /**
 555      * Converts this {@code TimeZone} object to a {@code ZoneId}.
 556      *
 557      * @return a {@code ZoneId} representing the same time zone as this
 558      *         {@code TimeZone}
 559      * @since 1.8
 560      */
 561     public ZoneId toZoneId() {
 562         return ZoneId.of(getID(), ZoneId.OLD_IDS_POST_2005);
 563     }
 564 
 565     private static TimeZone getTimeZone(String ID, boolean fallback) {
 566         TimeZone tz = ZoneInfo.getTimeZone(ID);
 567         if (tz == null) {
 568             tz = parseCustomTimeZone(ID);
 569             if (tz == null && fallback) {
 570                 tz = new ZoneInfo(GMT_ID, 0);
 571             }
 572         }
 573         return tz;
 574     }
 575 
 576     /**
 577      * Gets the available IDs according to the given time zone offset in milliseconds.
 578      *
 579      * @param rawOffset the given time zone GMT offset in milliseconds.
 580      * @return an array of IDs, where the time zone for that ID has
 581      * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
 582      * both have GMT-07:00, but differ in daylight saving behavior.




 542      */
 543     public static TimeZone getTimeZone(ZoneId zoneId) {
 544         String tzid = zoneId.getId(); // throws an NPE if null
 545         char c = tzid.charAt(0);
 546         if (c == '+' || c == '-') {
 547             tzid = "GMT" + tzid;
 548         } else if (c == 'Z' && tzid.length() == 1) {
 549             tzid = "UTC";
 550         }
 551         return getTimeZone(tzid, true);
 552     }
 553 
 554     /**
 555      * Converts this {@code TimeZone} object to a {@code ZoneId}.
 556      *
 557      * @return a {@code ZoneId} representing the same time zone as this
 558      *         {@code TimeZone}
 559      * @since 1.8
 560      */
 561     public ZoneId toZoneId() {
 562         return ZoneId.of(getID(), ZoneId.SHORT_IDS);
 563     }
 564 
 565     private static TimeZone getTimeZone(String ID, boolean fallback) {
 566         TimeZone tz = ZoneInfo.getTimeZone(ID);
 567         if (tz == null) {
 568             tz = parseCustomTimeZone(ID);
 569             if (tz == null && fallback) {
 570                 tz = new ZoneInfo(GMT_ID, 0);
 571             }
 572         }
 573         return tz;
 574     }
 575 
 576     /**
 577      * Gets the available IDs according to the given time zone offset in milliseconds.
 578      *
 579      * @param rawOffset the given time zone GMT offset in milliseconds.
 580      * @return an array of IDs, where the time zone for that ID has
 581      * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
 582      * both have GMT-07:00, but differ in daylight saving behavior.