Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
      4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      5  *
      6  * This code is free software; you can redistribute it and/or modify it
      7  * under the terms of the GNU General Public License version 2 only, as
      8  * published by the Free Software Foundation.  Oracle designates this
      9  * particular file as subject to the "Classpath" exception as provided
     10  * by Oracle in the LICENSE file that accompanied this code.
     11  *
     12  * This code is distributed in the hope that it will be useful, but WITHOUT
     13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     15  * version 2 for more details (a copy is included in the LICENSE file that
     16  * accompanied this code).
     17  *
     18  * You should have received a copy of the GNU General Public License version
     19  * 2 along with this work; if not, write to the Free Software Foundation,
     20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     21  *
     22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     23  * or visit www.oracle.com if you need additional information or have any
     24  * questions.
     25  */
     26 
     27 /*
     28  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
     29  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
     30  *
     31  *   The original version of this source code and documentation is copyrighted
     32  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
     33  * materials are provided under terms of a License Agreement between Taligent
     34  * and Sun. This technology is protected by multiple US and International
     35  * patents. This notice and attribution to Taligent may not be removed.
     36  *   Taligent is a registered trademark of Taligent, Inc.
     37  *
     38  */
     39 
     40 package java.util;
     41 
     42 import org.apache.harmony.luni.internal.util.TimezoneGetter;
     43 import android.icu.text.TimeZoneNames;
     44 import java.io.IOException;
     45 import java.io.Serializable;
     46 import java.time.ZoneId;
     47 import java.util.regex.Matcher;
     48 import java.util.regex.Pattern;
     49 import libcore.io.IoUtils;
     50 import libcore.util.ZoneInfoDB;
     51 
     52 /**
     53  * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
     54  * savings.
     55  *
     56  * <p>
     57  * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
     58  * which creates a <code>TimeZone</code> based on the time zone where the program
     59  * is running. For example, for a program running in Japan, <code>getDefault</code>
     60  * creates a <code>TimeZone</code> object based on Japanese Standard Time.
     61  *
     62  * <p>
     63  * You can also get a <code>TimeZone</code> using <code>getTimeZone</code>
     64  * along with a time zone ID. For instance, the time zone ID for the
     65  * U.S. Pacific Time zone is "America/Los_Angeles". So, you can get a
     66  * U.S. Pacific Time <code>TimeZone</code> object with:
     67  * <blockquote><pre>
     68  * TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
     69  * </pre></blockquote>
     70  * You can use the <code>getAvailableIDs</code> method to iterate through
     71  * all the supported time zone IDs. You can then choose a
     72  * supported ID to get a <code>TimeZone</code>.
     73  * If the time zone you want is not represented by one of the
     74  * supported IDs, then a custom time zone ID can be specified to
     75  * produce a TimeZone. The syntax of a custom time zone ID is:
     76  *
     77  * <blockquote><pre>
     78  * <a name="CustomID"><i>CustomID:</i></a>
     79  *         <code>GMT</code> <i>Sign</i> <i>Hours</i> <code>:</code> <i>Minutes</i>
     80  *         <code>GMT</code> <i>Sign</i> <i>Hours</i> <i>Minutes</i>
     81  *         <code>GMT</code> <i>Sign</i> <i>Hours</i>
     82  * <i>Sign:</i> one of
     83  *         <code>+ -</code>
     84  * <i>Hours:</i>
     85  *         <i>Digit</i>
     86  *         <i>Digit</i> <i>Digit</i>
     87  * <i>Minutes:</i>
     88  *         <i>Digit</i> <i>Digit</i>
     89  * <i>Digit:</i> one of
     90  *         <code>0 1 2 3 4 5 6 7 8 9</code>
     91  * </pre></blockquote>
     92  *
     93  * <i>Hours</i> must be between 0 to 23 and <i>Minutes</i> must be
     94  * between 00 to 59.  For example, "GMT+10" and "GMT+0010" mean ten
     95  * hours and ten minutes ahead of GMT, respectively.
     96  * <p>
     97  * The format is locale independent and digits must be taken from the
     98  * Basic Latin block of the Unicode standard. No daylight saving time
     99  * transition schedule can be specified with a custom time zone ID. If
    100  * the specified string doesn't match the syntax, <code>"GMT"</code>
    101  * is used.
    102  * <p>
    103  * When creating a <code>TimeZone</code>, the specified custom time
    104  * zone ID is normalized in the following syntax:
    105  * <blockquote><pre>
    106  * <a name="NormalizedCustomID"><i>NormalizedCustomID:</i></a>
    107  *         <code>GMT</code> <i>Sign</i> <i>TwoDigitHours</i> <code>:</code> <i>Minutes</i>
    108  * <i>Sign:</i> one of
    109  *         <code>+ -</code>
    110  * <i>TwoDigitHours:</i>
    111  *         <i>Digit</i> <i>Digit</i>
    112  * <i>Minutes:</i>
    113  *         <i>Digit</i> <i>Digit</i>
    114  * <i>Digit:</i> one of
    115  *         <code>0 1 2 3 4 5 6 7 8 9</code>
    116  * </pre></blockquote>
    117  * For example, TimeZone.getTimeZone("GMT-8").getID() returns "GMT-08:00".
    118  *
    119  * <h3>Three-letter time zone IDs</h3>
    120  *
    121  * For compatibility with JDK 1.1.x, some other three-letter time zone IDs
    122  * (such as "PST", "CTT", "AST") are also supported. However, <strong>their
    123  * use is deprecated</strong> because the same abbreviation is often used
    124  * for multiple time zones (for example, "CST" could be U.S. "Central Standard
    125  * Time" and "China Standard Time"), and the Java platform can then only
    126  * recognize one of them.
    127  *
    128  *
    129  * @see          Calendar
    130  * @see          GregorianCalendar
    131  * @see          SimpleTimeZone
    132  * @author       Mark Davis, David Goldsmith, Chen-Lieh Huang, Alan Liu
    133  * @since        JDK1.1
    134  */
    135 abstract public class TimeZone implements Serializable, Cloneable {
    136     /**
    137      * Sole constructor.  (For invocation by subclass constructors, typically
    138      * implicit.)
    139      */
    140     public TimeZone() {
    141     }
    142 
    143     /**
    144      * A style specifier for <code>getDisplayName()</code> indicating
    145      * a short name, such as "PST."
    146      * @see #LONG
    147      * @since 1.2
    148      */
    149     public static final int SHORT = 0;
    150 
    151     /**
    152      * A style specifier for <code>getDisplayName()</code> indicating
    153      * a long name, such as "Pacific Standard Time."
    154      * @see #SHORT
    155      * @since 1.2
    156      */
    157     public static final int LONG  = 1;
    158 
    159     // Android-changed: Use a preload holder to allow compile-time initialization of TimeZone and
    160     // dependents.
    161     private static class NoImagePreloadHolder {
    162         public static final Pattern CUSTOM_ZONE_ID_PATTERN = Pattern.compile("^GMT[-+](\\d{1,2})(:?(\\d\\d))?$");
    163     }
    164 
    165     // Proclaim serialization compatibility with JDK 1.1
    166     static final long serialVersionUID = 3581463369166924961L;
    167 
    168     // Android-changed: common timezone instances.
    169     private static final TimeZone GMT = new SimpleTimeZone(0, "GMT");
    170     private static final TimeZone UTC = new SimpleTimeZone(0, "UTC");
    171 
    172     /**
    173      * Gets the time zone offset, for current date, modified in case of
    174      * daylight savings. This is the offset to add to UTC to get local time.
    175      * <p>
    176      * This method returns a historically correct offset if an
    177      * underlying <code>TimeZone</code> implementation subclass
    178      * supports historical Daylight Saving Time schedule and GMT
    179      * offset changes.
    180      *
    181      * @param era the era of the given date.
    182      * @param year the year in the given date.
    183      * @param month the month in the given date.
    184      * Month is 0-based. e.g., 0 for January.
    185      * @param day the day-in-month of the given date.
    186      * @param dayOfWeek the day-of-week of the given date.
    187      * @param milliseconds the milliseconds in day in <em>standard</em>
    188      * local time.
    189      *
    190      * @return the offset in milliseconds to add to GMT to get local time.
    191      *
    192      * @see Calendar#ZONE_OFFSET
    193      * @see Calendar#DST_OFFSET
    194      */
    195     public abstract int getOffset(int era, int year, int month, int day,
    196                                   int dayOfWeek, int milliseconds);
    197 
    198     /**
    199      * Returns the offset of this time zone from UTC at the specified
    200      * date. If Daylight Saving Time is in effect at the specified
    201      * date, the offset value is adjusted with the amount of daylight
    202      * saving.
    203      * <p>
    204      * This method returns a historically correct offset value if an
    205      * underlying TimeZone implementation subclass supports historical
    206      * Daylight Saving Time schedule and GMT offset changes.
    207      *
    208      * @param date the date represented in milliseconds since January 1, 1970 00:00:00 GMT
    209      * @return the amount of time in milliseconds to add to UTC to get local time.
    210      *
    211      * @see Calendar#ZONE_OFFSET
    212      * @see Calendar#DST_OFFSET
    213      * @since 1.4
    214      */
    215     public int getOffset(long date) {
    216         if (inDaylightTime(new Date(date))) {
    217             return getRawOffset() + getDSTSavings();
    218         }
    219         return getRawOffset();
    220     }
    221 
    222     /**
    223      * Gets the raw GMT offset and the amount of daylight saving of this
    224      * time zone at the given time.
    225      * @param date the milliseconds (since January 1, 1970,
    226      * 00:00:00.000 GMT) at which the time zone offset and daylight
    227      * saving amount are found
    228      * @param offsets an array of int where the raw GMT offset
    229      * (offset[0]) and daylight saving amount (offset[1]) are stored,
    230      * or null if those values are not needed. The method assumes that
    231      * the length of the given array is two or larger.
    232      * @return the total amount of the raw GMT offset and daylight
    233      * saving at the specified date.
    234      *
    235      * @see Calendar#ZONE_OFFSET
    236      * @see Calendar#DST_OFFSET
    237      */
    238     int getOffsets(long date, int[] offsets) {
    239         int rawoffset = getRawOffset();
    240         int dstoffset = 0;
    241         if (inDaylightTime(new Date(date))) {
    242             dstoffset = getDSTSavings();
    243         }
    244         if (offsets != null) {
    245             offsets[0] = rawoffset;
    246             offsets[1] = dstoffset;
    247         }
    248         return rawoffset + dstoffset;
    249     }
    250 
    251     /**
    252      * Sets the base time zone offset to GMT.
    253      * This is the offset to add to UTC to get local time.
    254      * <p>
    255      * If an underlying <code>TimeZone</code> implementation subclass
    256      * supports historical GMT offset changes, the specified GMT
    257      * offset is set as the latest GMT offset and the difference from
    258      * the known latest GMT offset value is used to adjust all
    259      * historical GMT offset values.
    260      *
    261      * @param offsetMillis the given base time zone offset to GMT.
    262      */
    263     abstract public void setRawOffset(int offsetMillis);
    264 
    265     /**
    266      * Returns the amount of time in milliseconds to add to UTC to get
    267      * standard time in this time zone. Because this value is not
    268      * affected by daylight saving time, it is called <I>raw
    269      * offset</I>.
    270      * <p>
    271      * If an underlying <code>TimeZone</code> implementation subclass
    272      * supports historical GMT offset changes, the method returns the
    273      * raw offset value of the current date. In Honolulu, for example,
    274      * its raw offset changed from GMT-10:30 to GMT-10:00 in 1947, and
    275      * this method always returns -36000000 milliseconds (i.e., -10
    276      * hours).
    277      *
    278      * @return the amount of raw offset time in milliseconds to add to UTC.
    279      * @see Calendar#ZONE_OFFSET
    280      */
    281     public abstract int getRawOffset();
    282 
    283     /**
    284      * Gets the ID of this time zone.
    285      * @return the ID of this time zone.
    286      */
    287     public String getID()
    288     {
    289         return ID;
    290     }
    291 
    292     /**
    293      * Sets the time zone ID. This does not change any other data in
    294      * the time zone object.
    295      * @param ID the new time zone ID.
    296      */
    297     public void setID(String ID)
    298     {
    299         if (ID == null) {
    300             throw new NullPointerException();
    301         }
    302         this.ID = ID;
    303     }
    304 
    305     /**
    306      * Returns a long standard time name of this {@code TimeZone} suitable for
    307      * presentation to the user in the default locale.
    308      *
    309      * <p>This method is equivalent to:
    310      * <blockquote><pre>
    311      * getDisplayName(false, {@link #LONG},
    312      *                Locale.getDefault({@link Locale.Category#DISPLAY}))
    313      * </pre></blockquote>
    314      *
    315      * @return the human-readable name of this time zone in the default locale.
    316      * @since 1.2
    317      * @see #getDisplayName(boolean, int, Locale)
    318      * @see Locale#getDefault(Locale.Category)
    319      * @see Locale.Category
    320      */
    321     public final String getDisplayName() {
    322         return getDisplayName(false, LONG,
    323                               Locale.getDefault(Locale.Category.DISPLAY));
    324     }
    325 
    326     /**
    327      * Returns a long standard time name of this {@code TimeZone} suitable for
    328      * presentation to the user in the specified {@code locale}.
    329      *
    330      * <p>This method is equivalent to:
    331      * <blockquote><pre>
    332      * getDisplayName(false, {@link #LONG}, locale)
    333      * </pre></blockquote>
    334      *
    335      * @param locale the locale in which to supply the display name.
    336      * @return the human-readable name of this time zone in the given locale.
    337      * @exception NullPointerException if {@code locale} is {@code null}.
    338      * @since 1.2
    339      * @see #getDisplayName(boolean, int, Locale)
    340      */
    341     public final String getDisplayName(Locale locale) {
    342         return getDisplayName(false, LONG, locale);
    343     }
    344 
    345     /**
    346      * Returns a name in the specified {@code style} of this {@code TimeZone}
    347      * suitable for presentation to the user in the default locale. If the
    348      * specified {@code daylight} is {@code true}, a Daylight Saving Time name
    349      * is returned (even if this {@code TimeZone} doesn't observe Daylight Saving
    350      * Time). Otherwise, a Standard Time name is returned.
    351      *
    352      * <p>This method is equivalent to:
    353      * <blockquote><pre>
    354      * getDisplayName(daylight, style,
    355      *                Locale.getDefault({@link Locale.Category#DISPLAY}))
    356      * </pre></blockquote>
    357      *
    358      * @param daylight {@code true} specifying a Daylight Saving Time name, or
    359      *                 {@code false} specifying a Standard Time name
    360      * @param style either {@link #LONG} or {@link #SHORT}
    361      * @return the human-readable name of this time zone in the default locale.
    362      * @exception IllegalArgumentException if {@code style} is invalid.
    363      * @since 1.2
    364      * @see #getDisplayName(boolean, int, Locale)
    365      * @see Locale#getDefault(Locale.Category)
    366      * @see Locale.Category
    367      * @see java.text.DateFormatSymbols#getZoneStrings()
    368      */
    369     public final String getDisplayName(boolean daylight, int style) {
    370         return getDisplayName(daylight, style,
    371                               Locale.getDefault(Locale.Category.DISPLAY));
    372     }
    373 
    374     /**
    375      * Returns the {@link #SHORT short} or {@link #LONG long} name of this time
    376      * zone with either standard or daylight time, as written in {@code locale}.
    377      * If the name is not available, the result is in the format
    378      * {@code GMT[+-]hh:mm}.
    379      *
    380      * @param daylightTime true for daylight time, false for standard time.
    381      * @param style either {@link TimeZone#LONG} or {@link TimeZone#SHORT}.
    382      * @param locale the display locale.
    383      */
    384     public String getDisplayName(boolean daylightTime, int style, Locale locale) {
    385         // BEGIN Android-changed: implement using android.icu.text.TimeZoneNames
    386         TimeZoneNames.NameType nameType;
    387         switch (style) {
    388             case SHORT:
    389                 nameType = daylightTime
    390                         ? TimeZoneNames.NameType.SHORT_DAYLIGHT
    391                         : TimeZoneNames.NameType.SHORT_STANDARD;
    392                 break;
    393             case LONG:
    394                 nameType = daylightTime
    395                         ? TimeZoneNames.NameType.LONG_DAYLIGHT
    396                         : TimeZoneNames.NameType.LONG_STANDARD;
    397                 break;
    398             default:
    399                 throw new IllegalArgumentException("Illegal style: " + style);
    400         }
    401         String canonicalID = android.icu.util.TimeZone.getCanonicalID(getID());
    402         if (canonicalID != null) {
    403             TimeZoneNames names = TimeZoneNames.getInstance(locale);
    404             long now = System.currentTimeMillis();
    405             String displayName = names.getDisplayName(canonicalID, nameType, now);
    406             if (displayName != null) {
    407                 return displayName;
    408             }
    409         }
    410 
    411         // We get here if this is a custom timezone or ICU doesn't have name data for the specific
    412         // style and locale.
    413         int offsetMillis = getRawOffset();
    414         if (daylightTime) {
    415             offsetMillis += getDSTSavings();
    416         }
    417         return createGmtOffsetString(true /* includeGmt */, true /* includeMinuteSeparator */,
    418                 offsetMillis);
    419         // END Android-changed: implement using android.icu.text.TimeZoneNames
    420     }
    421 
    422     // BEGIN Android-added: utility method to format an offset as a GMT offset string.
    423     /**
    424      * Returns a string representation of an offset from UTC.
    425      *
    426      * <p>The format is "[GMT](+|-)HH[:]MM". The output is not localized.
    427      *
    428      * @param includeGmt true to include "GMT", false to exclude
    429      * @param includeMinuteSeparator true to include the separator between hours and minutes, false
    430      *     to exclude.
    431      * @param offsetMillis the offset from UTC
    432      *
    433      * @hide used internally by SimpleDateFormat
    434      */
    435     public static String createGmtOffsetString(boolean includeGmt,
    436             boolean includeMinuteSeparator, int offsetMillis) {
    437         int offsetMinutes = offsetMillis / 60000;
    438         char sign = '+';
    439         if (offsetMinutes < 0) {
    440             sign = '-';
    441             offsetMinutes = -offsetMinutes;
    442         }
    443         StringBuilder builder = new StringBuilder(9);
    444         if (includeGmt) {
    445             builder.append("GMT");
    446         }
    447         builder.append(sign);
    448         appendNumber(builder, 2, offsetMinutes / 60);
    449         if (includeMinuteSeparator) {
    450             builder.append(':');
    451         }
    452         appendNumber(builder, 2, offsetMinutes % 60);
    453         return builder.toString();
    454     }
    455 
    456     private static void appendNumber(StringBuilder builder, int count, int value) {
    457         String string = Integer.toString(value);
    458         for (int i = 0; i < count - string.length(); i++) {
    459             builder.append('0');
    460         }
    461         builder.append(string);
    462     }
    463     // END Android-added: utility method to format an offset as a GMT offset string.
    464 
    465     /**
    466      * Returns the amount of time to be added to local standard time
    467      * to get local wall clock time.
    468      *
    469      * <p>The default implementation returns 3600000 milliseconds
    470      * (i.e., one hour) if a call to {@link #useDaylightTime()}
    471      * returns {@code true}. Otherwise, 0 (zero) is returned.
    472      *
    473      * <p>If an underlying {@code TimeZone} implementation subclass
    474      * supports historical and future Daylight Saving Time schedule
    475      * changes, this method returns the amount of saving time of the
    476      * last known Daylight Saving Time rule that can be a future
    477      * prediction.
    478      *
    479      * <p>If the amount of saving time at any given time stamp is
    480      * required, construct a {@link Calendar} with this {@code
    481      * TimeZone} and the time stamp, and call {@link Calendar#get(int)
    482      * Calendar.get}{@code (}{@link Calendar#DST_OFFSET}{@code )}.
    483      *
    484      * @return the amount of saving time in milliseconds
    485      * @since 1.4
    486      * @see #inDaylightTime(Date)
    487      * @see #getOffset(long)
    488      * @see #getOffset(int,int,int,int,int,int)
    489      * @see Calendar#ZONE_OFFSET
    490      */
    491     public int getDSTSavings() {
    492         if (useDaylightTime()) {
    493             return 3600000;
    494         }
    495         return 0;
    496     }
    497 
    498     /**
    499      * Queries if this {@code TimeZone} uses Daylight Saving Time.
    500      *
    501      * <p>If an underlying {@code TimeZone} implementation subclass
    502      * supports historical and future Daylight Saving Time schedule
    503      * changes, this method refers to the last known Daylight Saving Time
    504      * rule that can be a future prediction and may not be the same as
    505      * the current rule. Consider calling {@link #observesDaylightTime()}
    506      * if the current rule should also be taken into account.
    507      *
    508      * @return {@code true} if this {@code TimeZone} uses Daylight Saving Time,
    509      *         {@code false}, otherwise.
    510      * @see #inDaylightTime(Date)
    511      * @see Calendar#DST_OFFSET
    512      */
    513     public abstract boolean useDaylightTime();
    514 
    515     /**
    516      * Returns {@code true} if this {@code TimeZone} is currently in
    517      * Daylight Saving Time, or if a transition from Standard Time to
    518      * Daylight Saving Time occurs at any future time.
    519      *
    520      * <p>The default implementation returns {@code true} if
    521      * {@code useDaylightTime()} or {@code inDaylightTime(new Date())}
    522      * returns {@code true}.
    523      *
    524      * @return {@code true} if this {@code TimeZone} is currently in
    525      * Daylight Saving Time, or if a transition from Standard Time to
    526      * Daylight Saving Time occurs at any future time; {@code false}
    527      * otherwise.
    528      * @since 1.7
    529      * @see #useDaylightTime()
    530      * @see #inDaylightTime(Date)
    531      * @see Calendar#DST_OFFSET
    532      */
    533     public boolean observesDaylightTime() {
    534         return useDaylightTime() || inDaylightTime(new Date());
    535     }
    536 
    537     /**
    538      * Queries if the given {@code date} is in Daylight Saving Time in
    539      * this time zone.
    540      *
    541      * @param date the given Date.
    542      * @return {@code true} if the given date is in Daylight Saving Time,
    543      *         {@code false}, otherwise.
    544      */
    545     abstract public boolean inDaylightTime(Date date);
    546 
    547     /**
    548      * Gets the <code>TimeZone</code> for the given ID.
    549      *
    550      * @param id the ID for a <code>TimeZone</code>, either an abbreviation
    551      * such as "PST", a full name such as "America/Los_Angeles", or a custom
    552      * ID such as "GMT-8:00". Note that the support of abbreviations is
    553      * for JDK 1.1.x compatibility only and full names should be used.
    554      *
    555      * @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
    556      * cannot be understood.
    557      */
    558     // Android-changed: param s/ID/id; use ZoneInfoDB instead of ZoneInfo class.
    559     public static synchronized TimeZone getTimeZone(String id) {
    560         if (id == null) {
    561             throw new NullPointerException("id == null");
    562         }
    563 
    564         // Special cases? These can clone an existing instance.
    565         if (id.length() == 3) {
    566             if (id.equals("GMT")) {
    567                 return (TimeZone) GMT.clone();
    568             }
    569             if (id.equals("UTC")) {
    570                 return (TimeZone) UTC.clone();
    571             }
    572         }
    573 
    574         // In the database?
    575         TimeZone zone = null;
    576         try {
    577             zone = ZoneInfoDB.getInstance().makeTimeZone(id);
    578         } catch (IOException ignored) {
    579         }
    580 
    581         // Custom time zone?
    582         if (zone == null && id.length() > 3 && id.startsWith("GMT")) {
    583             zone = getCustomTimeZone(id);
    584         }
    585 
    586         // We never return null; on failure we return the equivalent of "GMT".
    587         return (zone != null) ? zone : (TimeZone) GMT.clone();
    588     }
    589 
    590     /**
    591      * Gets the {@code TimeZone} for the given {@code zoneId}.
    592      *
    593      * @param zoneId a {@link ZoneId} from which the time zone ID is obtained
    594      * @return the specified {@code TimeZone}, or the GMT zone if the given ID
    595      *         cannot be understood.
    596      * @throws NullPointerException if {@code zoneId} is {@code null}
    597      * @since 1.8
    598      */
    599     public static TimeZone getTimeZone(ZoneId zoneId) {
    600         String tzid = zoneId.getId(); // throws an NPE if null
    601         char c = tzid.charAt(0);
    602         if (c == '+' || c == '-') {
    603             tzid = "GMT" + tzid;
    604         } else if (c == 'Z' && tzid.length() == 1) {
    605             tzid = "UTC";
    606         }
    607         return getTimeZone(tzid);
    608     }
    609 
    610     /**
    611      * Converts this {@code TimeZone} object to a {@code ZoneId}.
    612      *
    613      * @return a {@code ZoneId} representing the same time zone as this
    614      *         {@code TimeZone}
    615      * @since 1.8
    616      */
    617     public ZoneId toZoneId() {
    618         // Android-changed: don't support "old mapping"
    619         return ZoneId.of(getID(), ZoneId.SHORT_IDS);
    620     }
    621 
    622     /**
    623      * Returns a new SimpleTimeZone for an ID of the form "GMT[+|-]hh[[:]mm]", or null.
    624      */
    625     private static TimeZone getCustomTimeZone(String id) {
    626         Matcher m = NoImagePreloadHolder.CUSTOM_ZONE_ID_PATTERN.matcher(id);
    627         if (!m.matches()) {
    628             return null;
    629         }
    630 
    631         int hour;
    632         int minute = 0;
    633         try {
    634             hour = Integer.parseInt(m.group(1));
    635             if (m.group(3) != null) {
    636                 minute = Integer.parseInt(m.group(3));
    637             }
    638         } catch (NumberFormatException impossible) {
    639             throw new AssertionError(impossible);
    640         }
    641 
    642         if (hour < 0 || hour > 23 || minute < 0 || minute > 59) {
    643             return null;
    644         }
    645 
    646         char sign = id.charAt(3);
    647         int raw = (hour * 3600000) + (minute * 60000);
    648         if (sign == '-') {
    649             raw = -raw;
    650         }
    651 
    652         String cleanId = String.format(Locale.ROOT, "GMT%c%02d:%02d", sign, hour, minute);
    653 
    654         return new SimpleTimeZone(raw, cleanId);
    655     }
    656 
    657     /**
    658      * Gets the available IDs according to the given time zone offset in milliseconds.
    659      *
    660      * @param rawOffset the given time zone GMT offset in milliseconds.
    661      * @return an array of IDs, where the time zone for that ID has
    662      * the specified GMT offset. For example, "America/Phoenix" and "America/Denver"
    663      * both have GMT-07:00, but differ in daylight saving behavior.
    664      * @see #getRawOffset()
    665      */
    666     public static synchronized String[] getAvailableIDs(int rawOffset) {
    667         return ZoneInfoDB.getInstance().getAvailableIDs(rawOffset);
    668     }
    669 
    670     /**
    671      * Gets all the available IDs supported.
    672      * @return an array of IDs.
    673      */
    674     public static synchronized String[] getAvailableIDs() {
    675         return ZoneInfoDB.getInstance().getAvailableIDs();
    676     }
    677 
    678     /**
    679      * Gets the platform defined TimeZone ID.
    680      **/
    681     private static native String getSystemTimeZoneID(String javaHome,
    682                                                      String country);
    683 
    684     /**
    685      * Gets the custom time zone ID based on the GMT offset of the
    686      * platform. (e.g., "GMT+08:00")
    687      */
    688     private static native String getSystemGMTOffsetID();
    689 
    690     /**
    691      * Gets the default <code>TimeZone</code> for this host.
    692      * The source of the default <code>TimeZone</code>
    693      * may vary with implementation.
    694      * @return a default <code>TimeZone</code>.
    695      * @see #setDefault
    696      */
    697     public static TimeZone getDefault() {
    698         return (TimeZone) getDefaultRef().clone();
    699     }
    700 
    701     /**
    702      * Returns the reference to the default TimeZone object. This
    703      * method doesn't create a clone.
    704      */
    705     static synchronized TimeZone getDefaultRef() {
    706         if (defaultTimeZone == null) {
    707             TimezoneGetter tzGetter = TimezoneGetter.getInstance();
    708             String zoneName = (tzGetter != null) ? tzGetter.getId() : null;
    709             if (zoneName != null) {
    710                 zoneName = zoneName.trim();
    711             }
    712             if (zoneName == null || zoneName.isEmpty()) {
    713                 try {
    714                     // On the host, we can find the configured timezone here.
    715                     zoneName = IoUtils.readFileAsString("/etc/timezone");
    716                 } catch (IOException ex) {
    717                     // "vogar --mode device" can end up here.
    718                     // TODO: give libcore access to Android system properties and read "persist.sys.timezone".
    719                     zoneName = "GMT";
    720                 }
    721             }
    722             defaultTimeZone = TimeZone.getTimeZone(zoneName);
    723         }
    724         return defaultTimeZone;
    725     }
    726 
    727     /**
    728      * Sets the {@code TimeZone} that is returned by the {@code getDefault}
    729      * method. {@code timeZone} is cached. If {@code timeZone} is null, the cached
    730      * default {@code TimeZone} is cleared. This method doesn't change the value
    731      * of the {@code user.timezone} property.
    732      *
    733      * @param timeZone the new default {@code TimeZone}, or null
    734      * @see #getDefault
    735      */
    736     // Android-changed: s/zone/timeZone, synchronized, removed mention of SecurityException
    737     public synchronized static void setDefault(TimeZone timeZone)
    738     {
    739         SecurityManager sm = System.getSecurityManager();
    740         if (sm != null) {
    741             sm.checkPermission(new PropertyPermission
    742                     ("user.timezone", "write"));
    743         }
    744         defaultTimeZone = timeZone != null ? (TimeZone) timeZone.clone() : null;
    745         // Android-changed: notify ICU4J of changed default TimeZone.
    746         android.icu.util.TimeZone.clearCachedDefault();
    747     }
    748 
    749     /**
    750      * Returns true if this zone has the same rule and offset as another zone.
    751      * That is, if this zone differs only in ID, if at all.  Returns false
    752      * if the other zone is null.
    753      * @param other the <code>TimeZone</code> object to be compared with
    754      * @return true if the other zone is not null and is the same as this one,
    755      * with the possible exception of the ID
    756      * @since 1.2
    757      */
    758     public boolean hasSameRules(TimeZone other) {
    759         return other != null && getRawOffset() == other.getRawOffset() &&
    760             useDaylightTime() == other.useDaylightTime();
    761     }
    762 
    763     /**
    764      * Creates a copy of this <code>TimeZone</code>.
    765      *
    766      * @return a clone of this <code>TimeZone</code>
    767      */
    768     public Object clone()
    769     {
    770         try {
    771             TimeZone other = (TimeZone) super.clone();
    772             other.ID = ID;
    773             return other;
    774         } catch (CloneNotSupportedException e) {
    775             throw new InternalError(e);
    776         }
    777     }
    778 
    779     /**
    780      * The null constant as a TimeZone.
    781      */
    782     static final TimeZone NO_TIMEZONE = null;
    783 
    784     // =======================privates===============================
    785 
    786     /**
    787      * The string identifier of this <code>TimeZone</code>.  This is a
    788      * programmatic identifier used internally to look up <code>TimeZone</code>
    789      * objects from the system table and also to map them to their localized
    790      * display names.  <code>ID</code> values are unique in the system
    791      * table but may not be for dynamically created zones.
    792      * @serial
    793      */
    794     private String           ID;
    795     private static volatile TimeZone defaultTimeZone;
    796 }
    797