java - JDK6 中的 TimeZone.setDefault 变化

标签 java timezone default java-6 java-5

我刚刚注意到 JDK 6 与 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);
 }

最佳答案

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

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

还有(重新文档):

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

总结:JDK 1.5 是规则的一个异常(exception),JDK 1.6 一切都恢复“正常”,根据文档,这是 VM 范围内的时区更改。

关于java - JDK6 中的 TimeZone.setDefault 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2176784/

相关文章:

java - 类内部的序列化

java - joda Chronology 和 DateTimeZone 之间有什么区别?

lambda - 在 Web2py 中使用 lambda 作为模型默认值

java - 在 Java EE 应用程序中从文件系统加载属性文件

java - array(or ArrayList) 和 LinkedList 在迭代时执行相同的操作吗?

datetime - 如果未指定,XML 架构 dateTime 的默认时区是什么?

c - 查找默认网关 ip 地址,而不是解析 proc 文件系统

ios - 分组的 UITableView 圆角半径默认值

Java/Swing : Why is my JFileChooser opening twice?

PHP DateTime 时区 - 构造函数与 Setter 方法