我的java程序需要使用当前本地时区记录事件。问题是,用户可以更改时区,但即使更改了系统时区,java程序也会继续使用“旧”时区运行。
我怀疑Java VM在运行java程序时将默认设置为时区(Timezone.setDefault()
的javadoc表示“将默认值重置为VM首次启动时的默认值。”) />
这是真的吗? Java无法将默认时区更新为新的系统时区,因此需要重新启动VM?
答案 0 :(得分:1)
为什么不将时区设置为声明的变量?将其初始化为默认时区,然后如果用户更改它,则将其保存到变量中。在日志中调用变量。
答案 1 :(得分:0)
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]]