我的Java程序需要使用当前本地时区记录事件。问题是,用户可以更改时区,但是即使系统时区已更改,Java程序仍会以“旧”时区运行。
我怀疑Java虚拟机在运行Java程序时将时区设置为默认时区(Timezone.setDefault()
的javadoc说“将默认值重置为虚拟机首次启动时的原始值。”)
真的吗? Java无法将默认时区更新为新的系统时区,因此需要重启VM?
最佳答案
while (true) {
Thread.sleep(1000);
System.out.println(TimeZone.getDefault());
System.getProperties().setProperty("user.timezone", "");
TimeZone.setDefault(null);
System.out.println(TimeZone.getDefault());
}
这是输出:
sun.util.calendar.ZoneInfo [id =“Asia/Taipei”,offset = 28800000,dstSavings = 0,useDaylight = false,transitions = 42,lastRule = null]
sun.util.calendar.ZoneInfo [id =“Asia/Hovd”,offset = 25200000,dstSavings = 0,useDaylight = false,transitions = 52,lastRule = null]
sun.util.calendar.ZoneInfo [id =“Asia/Hovd”,offset = 25200000,dstSavings = 0,useDaylight = false,transitions = 52,lastRule = null]
sun.util.calendar.ZoneInfo [id =“Asia/Hovd”,offset = 25200000,dstSavings = 0,useDaylight = false,transitions = 52,lastRule = null]
sun.util.calendar.ZoneInfo [id =“Asia/Hovd”,offset = 25200000,dstSavings = 0,useDaylight = false,transitions = 52,lastRule = null]
sun.util.calendar.ZoneInfo [id =“Europe/Kiev”,offset = 7200000,dstSavings = 3600000,useDaylight = true,transitions = 121,lastRule = java.util.SimpleTimeZone [id = Europe/Kiev,offset = 7200000, dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 2,startMonth = 2,startDay = -1,startDayOfWeek = 1,startTime = 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endDay = -1, endDayOfWeek = 1,endTime = 3600000,endTimeMode = 2]]
sun.util.calendar.ZoneInfo [id =“Europe/Kiev”,offset = 7200000,dstSavings = 3600000,useDaylight = true,transitions = 121,lastRule = java.util.SimpleTimeZone [id = Europe/Kiev,offset = 7200000, dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 2,startMonth = 2,startDay = -1,startDayOfWeek = 1,startTime = 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endDay = -1, endDayOfWeek = 1,endTime = 3600000,endTimeMode = 2]]
sun.util.calendar.ZoneInfo [id =“Europe/Kiev”,offset = 7200000,dstSavings = 3600000,useDaylight = true,transitions = 121,lastRule = java.util.SimpleTimeZone [id = Europe/Kiev,offset = 7200000, dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 2,startMonth = 2,startDay = -1,startDayOfWeek = 1,startTime = 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endDay = -1, endDayOfWeek = 1,endTime = 3600000,endTimeMode = 2]]
sun.util.calendar.ZoneInfo [id =“Europe/Kiev”,offset = 7200000,dstSavings = 3600000,useDaylight = true,transitions = 121,lastRule = java.util.SimpleTimeZone [id = Europe/Kiev,offset = 7200000, dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 2,startMonth = 2,startDay = -1,startDayOfWeek = 1,startTime = 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endDay = -1, endDayOfWeek = 1,endTime = 3600000,endTimeMode = 2]]
sun.util.calendar.ZoneInfo [id =“Europe/Kiev”,offset = 7200000,dstSavings = 3600000,useDaylight = true,transitions = 121,lastRule = java.util.SimpleTimeZone [id = Europe/Kiev,offset = 7200000, dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 2,startMonth = 2,startDay = -1,startDayOfWeek = 1,startTime = 3600000,startTimeMode = 2,endMode = 2,endMonth = 9,endDay = -1, endDayOfWeek = 1,endTime = 3600000,endTimeMode = 2]]
sun.util.calendar.ZoneInfo [id =“America/Toronto”,offset = -18000000,dstSavings = 3600000,useDaylight = true,transitions = 231,lastRule = java.util.SimpleTimeZone [id = America/Toronto,offset =- 18000000,dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 3,startMonth = 2,startDay = 8,startDayOfWeek = 1,startTime = 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endDay = 1, endDayOfWeek = 1,endTime = 7200000,endTimeMode = 0]]
sun.util.calendar.ZoneInfo [id =“America/Toronto”,offset = -18000000,dstSavings = 3600000,useDaylight = true,transitions = 231,lastRule = java.util.SimpleTimeZone [id = America/Toronto,offset =- 18000000,dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 3,startMonth = 2,startDay = 8,startDayOfWeek = 1,startTime = 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endDay = 1, endDayOfWeek = 1,endTime = 7200000,endTimeMode = 0]]
sun.util.calendar.ZoneInfo [id =“America/Toronto”,offset = -18000000,dstSavings = 3600000,useDaylight = true,transitions = 231,lastRule = java.util.SimpleTimeZone [id = America/Toronto,offset =- 18000000,dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 3,startMonth = 2,startDay = 8,startDayOfWeek = 1,startTime = 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endDay = 1, endDayOfWeek = 1,endTime = 7200000,endTimeMode = 0]]
sun.util.calendar.ZoneInfo [id =“America/Toronto”,offset = -18000000,dstSavings = 3600000,useDaylight = true,transitions = 231,lastRule = java.util.SimpleTimeZone [id = America/Toronto,offset =- 18000000,dstSavings = 3600000,useDaylight = true,startYear = 0,startMode = 3,startMonth = 2,startDay = 8,startDayOfWeek = 1,startTime = 7200000,startTimeMode = 0,endMode = 3,endMonth = 10,endDay = 1, endDayOfWeek = 1,endTime = 7200000,endTimeMode = 0]]