Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java timezone change at runtime

My java program needs to log events with the current local timezone. The problem is, the user has the ability to change the timezone, but even if the system timezone is changed, the java program keeps running with the "old" timezone.

I suspect that the Java VM sets as default the timezone when the java program is run (the javadoc for Timezone.setDefault() says "reset the default to the value it had originally when the VM first started.")

Is that true? Java can't update the default timezone to the new system timezone and therefore requires a restart of the VM?

like image 728
michelemarcon Avatar asked Apr 07 '14 15:04

michelemarcon


1 Answers

while (true) {
    Thread.sleep(1000);
    System.out.println(TimeZone.getDefault());
    System.getProperties().setProperty("user.timezone", "");
    TimeZone.setDefault(null);
    System.out.println(TimeZone.getDefault());
}

Here is the output :

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]]

like image 198
Eason Xiao Avatar answered Sep 22 '22 13:09

Eason Xiao