17

我刚刚注意到 JDK 6 设置默认 TimeZone 的方法与 JDK5 不同。

以前,新的默认值将存储在线程局部变量中。使用 JDK6(我刚刚查看了 1.6.0.18),实现发生了变化,因此如果用户可以写入“user.timezone”属性,或者如果没有安装 SecurityManager,则时区会在 VM 范围内更改!否则会发生线程局部更改。

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

这是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);
 }
4

3 回答 3

13

搜索错误数据库实际上是个好主意 :)

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

还有(重新文档):

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

摘要:JDK 1.5 是该规则的一个例外,JDK 1.6 一切都恢复了“正常”,根据文档,时区更改是 VM 范围内的。

于 2010-02-01T14:01:20.663 回答
4

这可能是为了修复一个错误。我会搜索bugs.sun.com以找到它的基本原理。(线索也可以在发行说明中找到。)

于 2010-02-01T13:37:08.850 回答
1

TimeZone.getDefault() 的 API 文档指出“默认 TimeZone 的来源可能因实现而异”。如果您的代码依赖于标准 API 类的实现特定行为(在这种情况下,默认时区保持在线程本地级别),您必须预期您的代码会因 VM 的较新版本或来自不同的 VM 而失败供应商。

于 2010-02-01T13:46:14.383 回答