代码之家  ›  专栏  ›  技术社区  ›  Thomas Auinger

jdk6中的timezone.setdefault更改

  •  17
  • Thomas Auinger  · 技术社区  · 15 年前

    我刚刚注意到JDK6与JDK5在设置默认时区方面有不同的方法。

    以前,新的默认值将存储在线程局部变量中。有了JDK6(我刚刚回顾了1.6.0.18),实现已经改变了,所以如果用户可以写入“user.timezone”属性,或者如果没有安装SecurityManager,那么timezone将在虚拟机范围内改变!否则会发生线程局部更改。

    我错了吗?这似乎是一个巨大的变化,我在网上找不到任何关于它的信息。

    这里是JDK6代码:

     private static boolean hasPermission() {
      boolean hasPermission = true;
      SecurityManager sm = System.getSecurityManager();
      if (sm != null) {
       try {
        sm.checkPermission(new PropertyPermission("user.timezone", "write"));
       } catch (SecurityException e) {
        hasPermission = false;
       }
      }
      return hasPermission;
     }
    
     /**
      * Sets the <code>TimeZone</code> that is
      * returned by the <code>getDefault</code> method.  If <code>zone</code>
      * is null, reset the default to the value it had originally when the
      * VM first started.
      * @param zone the new default time zone
      * @see #getDefault
      */
     public static void setDefault(TimeZone zone)
     {
      if (hasPermission()) {
       synchronized (TimeZone.class) {
        defaultTimeZone = zone;
        defaultZoneTL.set(null);
       }
      } else {
       defaultZoneTL.set(zone);
      }
     }
    

    而在JDK5之前,它只是:

     /**
      * Sets the <code>TimeZone</code> that is
      * returned by the <code>getDefault</code> method.  If <code>zone</code>
      * is null, reset the default to the value it had originally when the
      * VM first started.
      * @param zone the new default time zone
      * @see #getDefault
      */
     public static synchronized void setDefault(TimeZone zone)
     {
      defaultZoneTL.set(zone);
     }
    
    3 回复  |  直到 15 年前
        1
  •  13
  •   Thomas Auinger    15 年前

    搜索bugs数据库实际上是个不错的主意:)

    http://bugs.sun.com/view_bug.do?bug_id=6352812

    以及(重新文档):

    http://bugs.sun.com/view_bug.do?bug_id=6181786

    总结:JDK1.5是这个规则的一个例外,JDK1.6的事情又回到了“正常”状态,根据文档,这就是时区的变化是虚拟机范围内的。

        2
  •  4
  •   McDowell rahul gupta    15 年前

    这可能是为了修复一个bug。我会搜索 bugs.sun.com 找到它的理由。 (也可以在 release notes )

        3
  •  1
  •   jarnbjo    15 年前

    time zone.getdefault()的API文档声明“默认时区的源可能随实现而变化”。如果您的代码依赖于标准API类的特定于实现的行为(在本例中,默认时区保持在线程本地级别),则必须预料到您的代码会因更新版本的T而失败。虚拟机或来自不同供应商的虚拟机。