2010-02-01 13 views
16

Sadece JDK 6'nın JDK5'ten daha varsayılan bir TimeZone ayarlamada farklı bir yaklaşımı olduğunu fark ettim.JDK6 içinde ZamanZone.setDefault değişiklikleri

Önceden yeni varsayılan bir iş parçacığı yerel değişkeninde saklanır. JDK6 ile (az önce 1.6.0.18'i inceledim) uygulama değişti, böylece kullanıcı "user.timezone" özelliğine yazabiliyorsa veya SecurityManager yüklü değilse, zaman dilimi VM genişliğini değiştirir! Aksi halde iplik-yerel bir değişiklik meydana gelir.

Yanlış mıyım? Bu oldukça büyük bir değişiklik gibi görünüyor ve web'de bu konuda hiçbir şey bulamadım.

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); 
    } 
} 

önce ise (JDK5 olarak) sadece oldu: Bu, muhtemelen bir hata düzeltmek için yapıldığını

/** 
    * 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); 
} 

cevap

1

TimeZone.getDefault (API belgeleri) belirtmektedir "varsayılan kaynağı TimeZone uygulamasıyla değişebilir." Kodunuz standart API sınıflarının uygulamaya özel davranışına dayanıyorsa (bu durumda varsayılan saat dilimi yerel bir iş parçacığında tutulur), kodunuzun VM'nin daha yeni sürümlerinde veya VM'lerden farklı bir şekilde başarısız olmasını beklemeniz gerekir. satıcıları. böcek veritabanı aranıyor

İlgili konular