工作中,遇到一个Java读取默认时区的问题,后来看了openjdk的源码,大致整理一下过程
public classTest {public voidtest(){
TimeZone.getDefault();
}
}
TimeZone.getDefault()会跳到下面代码:
private static synchronizedTimeZone setDefaultZone() {
TimeZone tz;//get the time zone ID from the system properties
String zoneID =AccessController.doPrivileged(new GetPropertyAction("user.timezone"));//if the time zone ID is not set (yet), perform the//platform to Java time zone ID mapping.
if (zoneID == null ||zoneID.isEmpty()) {
String javaHome=AccessController.doPrivileged(new GetPropertyAction("java.home"));try{
zoneID=getSystemTimeZoneID(javaHome);if (zoneID == null) {
zoneID=GMT_ID;
}
}catch(NullPointerException e) {
zoneID=GMT_ID;
}