jdk/src/java.base/share/classes/java/util/TimeZone.java
changeset 30644 cc05b944b2fa
parent 29986 97167d851fc4
child 32649 2ee9017c7597
equal deleted inserted replaced
30643:1fcf87dbdc3c 30644:cc05b944b2fa
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   291     {
   291     {
   292         if (ID == null) {
   292         if (ID == null) {
   293             throw new NullPointerException();
   293             throw new NullPointerException();
   294         }
   294         }
   295         this.ID = ID;
   295         this.ID = ID;
       
   296         this.zoneId = null;   // invalidate cache
   296     }
   297     }
   297 
   298 
   298     /**
   299     /**
   299      * Returns a long standard time name of this {@code TimeZone} suitable for
   300      * Returns a long standard time name of this {@code TimeZone} suitable for
   300      * presentation to the user in the default locale.
   301      * presentation to the user in the default locale.
   542      * @return a {@code ZoneId} representing the same time zone as this
   543      * @return a {@code ZoneId} representing the same time zone as this
   543      *         {@code TimeZone}
   544      *         {@code TimeZone}
   544      * @since 1.8
   545      * @since 1.8
   545      */
   546      */
   546     public ZoneId toZoneId() {
   547     public ZoneId toZoneId() {
       
   548         ZoneId zId = zoneId;
       
   549         if (zId == null) {
       
   550             zoneId = zId = toZoneId0();
       
   551         }
       
   552         return zId;
       
   553     }
       
   554 
       
   555     private ZoneId toZoneId0() {
   547         String id = getID();
   556         String id = getID();
       
   557         TimeZone defaultZone = defaultTimeZone;
       
   558         // are we not defaultTimeZone but our id is equal to default's?
       
   559         if (defaultZone != this &&
       
   560             defaultZone != null && id.equals(defaultZone.getID())) {
       
   561             // delegate to default TZ which is effectively immutable
       
   562             return defaultZone.toZoneId();
       
   563         }
       
   564         // derive it ourselves
   548         if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
   565         if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
   549             if ("EST".equals(id))
   566             if ("EST".equals(id))
   550                 return ZoneId.of("America/New_York");
   567                 return ZoneId.of("America/New_York");
   551             if ("MST".equals(id))
   568             if ("MST".equals(id))
   552                 return ZoneId.of("America/Denver");
   569                 return ZoneId.of("America/Denver");
   708         SecurityManager sm = System.getSecurityManager();
   725         SecurityManager sm = System.getSecurityManager();
   709         if (sm != null) {
   726         if (sm != null) {
   710             sm.checkPermission(new PropertyPermission
   727             sm.checkPermission(new PropertyPermission
   711                                ("user.timezone", "write"));
   728                                ("user.timezone", "write"));
   712         }
   729         }
   713         defaultTimeZone = zone;
   730         // by saving a defensive clone and returning a clone in getDefault() too,
       
   731         // the defaultTimeZone instance is isolated from user code which makes it
       
   732         // effectively immutable. This is important to avoid races when the
       
   733         // following is evaluated in ZoneId.systemDefault():
       
   734         // TimeZone.getDefault().toZoneId().
       
   735         defaultTimeZone = (zone == null) ? null : (TimeZone) zone.clone();
   714     }
   736     }
   715 
   737 
   716     /**
   738     /**
   717      * Returns true if this zone has the same rule and offset as another zone.
   739      * Returns true if this zone has the same rule and offset as another zone.
   718      * That is, if this zone differs only in ID, if at all.  Returns false
   740      * That is, if this zone differs only in ID, if at all.  Returns false
   733      * @return a clone of this <code>TimeZone</code>
   755      * @return a clone of this <code>TimeZone</code>
   734      */
   756      */
   735     public Object clone()
   757     public Object clone()
   736     {
   758     {
   737         try {
   759         try {
   738             TimeZone other = (TimeZone) super.clone();
   760             return super.clone();
   739             other.ID = ID;
       
   740             return other;
       
   741         } catch (CloneNotSupportedException e) {
   761         } catch (CloneNotSupportedException e) {
   742             throw new InternalError(e);
   762             throw new InternalError(e);
   743         }
   763         }
   744     }
   764     }
   745 
   765 
   757      * display names.  <code>ID</code> values are unique in the system
   777      * display names.  <code>ID</code> values are unique in the system
   758      * table but may not be for dynamically created zones.
   778      * table but may not be for dynamically created zones.
   759      * @serial
   779      * @serial
   760      */
   780      */
   761     private String           ID;
   781     private String           ID;
       
   782 
       
   783     /**
       
   784      * Cached {@link ZoneId} for this TimeZone
       
   785      */
       
   786     private transient ZoneId zoneId;
       
   787 
   762     private static volatile TimeZone defaultTimeZone;
   788     private static volatile TimeZone defaultTimeZone;
   763 
   789 
   764     static final String         GMT_ID        = "GMT";
   790     static final String         GMT_ID        = "GMT";
   765     private static final int    GMT_ID_LENGTH = 3;
   791     private static final int    GMT_ID_LENGTH = 3;
   766 
   792