Tôi chỉ nhận thấy rằng JDK 6 có một cách tiếp cận khác để thiết lập một TimeZone mặc định hơn JDK5.TimeZone.setDefault thay đổi trong JDK6
Trước đây, mặc định mới sẽ được lưu trữ trong biến chủ đề cục bộ. Với JDK6 (tôi chỉ xem xét 1.6.0.18) việc thực hiện đã thay đổi, để nếu người dùng có thể ghi vào thuộc tính "user.timezone", hoặc nếu không có SecurityManager được cài đặt, múi giờ thay đổi toàn bộ VM! Nếu không, một thay đổi thread-local xảy ra.
Tôi có sai không? Điều này có vẻ là một thay đổi khá mạnh, và tôi không thể tìm thấy bất cứ điều gì trên web về nó.
Đây là mã 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);
}
}
thời gian trước khi (trong JDK5) nó chỉ đơn giản là:
/**
* 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);
}