276 /**
277 * Gets the ID of this time zone.
278 * @return the ID of this time zone.
279 */
280 public String getID()
281 {
282 return ID;
283 }
284
285 /**
286 * Sets the time zone ID. This does not change any other data in
287 * the time zone object.
288 * @param ID the new time zone ID.
289 */
290 public void setID(String ID)
291 {
292 if (ID == null) {
293 throw new NullPointerException();
294 }
295 this.ID = ID;
296 }
297
298 /**
299 * Returns a long standard time name of this {@code TimeZone} suitable for
300 * presentation to the user in the default locale.
301 *
302 * <p>This method is equivalent to:
303 * <blockquote><pre>
304 * getDisplayName(false, {@link #LONG},
305 * Locale.getDefault({@link Locale.Category#DISPLAY}))
306 * </pre></blockquote>
307 *
308 * @return the human-readable name of this time zone in the default locale.
309 * @since 1.2
310 * @see #getDisplayName(boolean, int, Locale)
311 * @see Locale#getDefault(Locale.Category)
312 * @see Locale.Category
313 */
314 public final String getDisplayName() {
315 return getDisplayName(false, LONG,
527 */
528 public static TimeZone getTimeZone(ZoneId zoneId) {
529 String tzid = zoneId.getId(); // throws an NPE if null
530 char c = tzid.charAt(0);
531 if (c == '+' || c == '-') {
532 tzid = "GMT" + tzid;
533 } else if (c == 'Z' && tzid.length() == 1) {
534 tzid = "UTC";
535 }
536 return getTimeZone(tzid, true);
537 }
538
539 /**
540 * Converts this {@code TimeZone} object to a {@code ZoneId}.
541 *
542 * @return a {@code ZoneId} representing the same time zone as this
543 * {@code TimeZone}
544 * @since 1.8
545 */
546 public ZoneId toZoneId() {
547 String id = getID();
548 if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
549 if ("EST".equals(id))
550 return ZoneId.of("America/New_York");
551 if ("MST".equals(id))
552 return ZoneId.of("America/Denver");
553 if ("HST".equals(id))
554 return ZoneId.of("America/Honolulu");
555 }
556 return ZoneId.of(id, ZoneId.SHORT_IDS);
557 }
558
559 private static TimeZone getTimeZone(String ID, boolean fallback) {
560 TimeZone tz = ZoneInfo.getTimeZone(ID);
561 if (tz == null) {
562 tz = parseCustomTimeZone(ID);
563 if (tz == null && fallback) {
564 tz = new ZoneInfo(GMT_ID, 0);
565 }
566 }
567 return tz;
693 /**
694 * Sets the {@code TimeZone} that is returned by the {@code getDefault}
695 * method. {@code zone} is cached. If {@code zone} is null, the cached
696 * default {@code TimeZone} is cleared. This method doesn't change the value
697 * of the {@code user.timezone} property.
698 *
699 * @param zone the new default {@code TimeZone}, or null
700 * @throws SecurityException if the security manager's {@code checkPermission}
701 * denies {@code PropertyPermission("user.timezone",
702 * "write")}
703 * @see #getDefault
704 * @see PropertyPermission
705 */
706 public static void setDefault(TimeZone zone)
707 {
708 SecurityManager sm = System.getSecurityManager();
709 if (sm != null) {
710 sm.checkPermission(new PropertyPermission
711 ("user.timezone", "write"));
712 }
713 defaultTimeZone = zone;
714 }
715
716 /**
717 * 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
719 * if the other zone is null.
720 * @param other the <code>TimeZone</code> object to be compared with
721 * @return true if the other zone is not null and is the same as this one,
722 * with the possible exception of the ID
723 * @since 1.2
724 */
725 public boolean hasSameRules(TimeZone other) {
726 return other != null && getRawOffset() == other.getRawOffset() &&
727 useDaylightTime() == other.useDaylightTime();
728 }
729
730 /**
731 * Creates a copy of this <code>TimeZone</code>.
732 *
733 * @return a clone of this <code>TimeZone</code>
734 */
735 public Object clone()
736 {
737 try {
738 TimeZone other = (TimeZone) super.clone();
739 other.ID = ID;
740 return other;
741 } catch (CloneNotSupportedException e) {
742 throw new InternalError(e);
743 }
744 }
745
746 /**
747 * The null constant as a TimeZone.
748 */
749 static final TimeZone NO_TIMEZONE = null;
750
751 // =======================privates===============================
752
753 /**
754 * The string identifier of this <code>TimeZone</code>. This is a
755 * programmatic identifier used internally to look up <code>TimeZone</code>
756 * objects from the system table and also to map them to their localized
757 * display names. <code>ID</code> values are unique in the system
758 * table but may not be for dynamically created zones.
759 * @serial
760 */
761 private String ID;
762 private static volatile TimeZone defaultTimeZone;
763
764 static final String GMT_ID = "GMT";
765 private static final int GMT_ID_LENGTH = 3;
766
767 // a static TimeZone we can reference if no AppContext is in place
768 private static volatile TimeZone mainAppContextDefault;
769
770 /**
771 * Parses a custom time zone identifier and returns a corresponding zone.
772 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
773 *
774 * @param id a string of the <a href="#CustomID">custom ID form</a>.
775 * @return a newly created TimeZone with the given offset and
776 * no daylight saving time, or null if the id cannot be parsed.
777 */
778 private static final TimeZone parseCustomTimeZone(String id) {
779 int length;
780
781 // Error if the length of id isn't long enough or id doesn't
|
276 /**
277 * Gets the ID of this time zone.
278 * @return the ID of this time zone.
279 */
280 public String getID()
281 {
282 return ID;
283 }
284
285 /**
286 * Sets the time zone ID. This does not change any other data in
287 * the time zone object.
288 * @param ID the new time zone ID.
289 */
290 public void setID(String ID)
291 {
292 if (ID == null) {
293 throw new NullPointerException();
294 }
295 this.ID = ID;
296 this.zoneId = null; // invalidate cache
297 }
298
299 /**
300 * Returns a long standard time name of this {@code TimeZone} suitable for
301 * presentation to the user in the default locale.
302 *
303 * <p>This method is equivalent to:
304 * <blockquote><pre>
305 * getDisplayName(false, {@link #LONG},
306 * Locale.getDefault({@link Locale.Category#DISPLAY}))
307 * </pre></blockquote>
308 *
309 * @return the human-readable name of this time zone in the default locale.
310 * @since 1.2
311 * @see #getDisplayName(boolean, int, Locale)
312 * @see Locale#getDefault(Locale.Category)
313 * @see Locale.Category
314 */
315 public final String getDisplayName() {
316 return getDisplayName(false, LONG,
528 */
529 public static TimeZone getTimeZone(ZoneId zoneId) {
530 String tzid = zoneId.getId(); // throws an NPE if null
531 char c = tzid.charAt(0);
532 if (c == '+' || c == '-') {
533 tzid = "GMT" + tzid;
534 } else if (c == 'Z' && tzid.length() == 1) {
535 tzid = "UTC";
536 }
537 return getTimeZone(tzid, true);
538 }
539
540 /**
541 * Converts this {@code TimeZone} object to a {@code ZoneId}.
542 *
543 * @return a {@code ZoneId} representing the same time zone as this
544 * {@code TimeZone}
545 * @since 1.8
546 */
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() {
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
562 return defaultZone.toZoneId();
563 }
564 // derive it ourselves
565 if (ZoneInfoFile.useOldMapping() && id.length() == 3) {
566 if ("EST".equals(id))
567 return ZoneId.of("America/New_York");
568 if ("MST".equals(id))
569 return ZoneId.of("America/Denver");
570 if ("HST".equals(id))
571 return ZoneId.of("America/Honolulu");
572 }
573 return ZoneId.of(id, ZoneId.SHORT_IDS);
574 }
575
576 private static TimeZone getTimeZone(String ID, boolean fallback) {
577 TimeZone tz = ZoneInfo.getTimeZone(ID);
578 if (tz == null) {
579 tz = parseCustomTimeZone(ID);
580 if (tz == null && fallback) {
581 tz = new ZoneInfo(GMT_ID, 0);
582 }
583 }
584 return tz;
710 /**
711 * Sets the {@code TimeZone} that is returned by the {@code getDefault}
712 * method. {@code zone} is cached. If {@code zone} is null, the cached
713 * default {@code TimeZone} is cleared. This method doesn't change the value
714 * of the {@code user.timezone} property.
715 *
716 * @param zone the new default {@code TimeZone}, or null
717 * @throws SecurityException if the security manager's {@code checkPermission}
718 * denies {@code PropertyPermission("user.timezone",
719 * "write")}
720 * @see #getDefault
721 * @see PropertyPermission
722 */
723 public static void setDefault(TimeZone zone)
724 {
725 SecurityManager sm = System.getSecurityManager();
726 if (sm != null) {
727 sm.checkPermission(new PropertyPermission
728 ("user.timezone", "write"));
729 }
730 // defensive clone
731 defaultTimeZone = (zone == null) ? null : (TimeZone) zone.clone();
732 }
733
734 /**
735 * Returns true if this zone has the same rule and offset as another zone.
736 * That is, if this zone differs only in ID, if at all. Returns false
737 * if the other zone is null.
738 * @param other the <code>TimeZone</code> object to be compared with
739 * @return true if the other zone is not null and is the same as this one,
740 * with the possible exception of the ID
741 * @since 1.2
742 */
743 public boolean hasSameRules(TimeZone other) {
744 return other != null && getRawOffset() == other.getRawOffset() &&
745 useDaylightTime() == other.useDaylightTime();
746 }
747
748 /**
749 * Creates a copy of this <code>TimeZone</code>.
750 *
751 * @return a clone of this <code>TimeZone</code>
752 */
753 public Object clone()
754 {
755 try {
756 return super.clone();
757 } catch (CloneNotSupportedException e) {
758 throw new InternalError(e);
759 }
760 }
761
762 /**
763 * The null constant as a TimeZone.
764 */
765 static final TimeZone NO_TIMEZONE = null;
766
767 // =======================privates===============================
768
769 /**
770 * The string identifier of this <code>TimeZone</code>. This is a
771 * programmatic identifier used internally to look up <code>TimeZone</code>
772 * objects from the system table and also to map them to their localized
773 * display names. <code>ID</code> values are unique in the system
774 * table but may not be for dynamically created zones.
775 * @serial
776 */
777 private String ID;
778
779 /**
780 * Cached {@link ZoneId} for this TimeZone
781 */
782 private transient ZoneId zoneId;
783
784 private static volatile TimeZone defaultTimeZone;
785
786 static final String GMT_ID = "GMT";
787 private static final int GMT_ID_LENGTH = 3;
788
789 // a static TimeZone we can reference if no AppContext is in place
790 private static volatile TimeZone mainAppContextDefault;
791
792 /**
793 * Parses a custom time zone identifier and returns a corresponding zone.
794 * This method doesn't support the RFC 822 time zone format. (e.g., +hhmm)
795 *
796 * @param id a string of the <a href="#CustomID">custom ID form</a>.
797 * @return a newly created TimeZone with the given offset and
798 * no daylight saving time, or null if the id cannot be parsed.
799 */
800 private static final TimeZone parseCustomTimeZone(String id) {
801 int length;
802
803 // Error if the length of id isn't long enough or id doesn't
|