8

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?

2
  • In java 7, setDefault takes a zone argument, and only resets to the original if you pass in a null: "If zone is null, reset the default to the value it had originally when the VM first started." API here Commented Apr 7, 2014 at 16:04
  • Better to do all your logging in UTC using the Instant class. You can present each recorded moment to the user in any time zone they wish. Passing a ZoneId object to Instant::atZone produces a ZonedDateTime. Just like localization, what you store and what you present can be two different things. Commented Nov 13, 2019 at 18:25

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Why not set the timezone to a declared variable? Initialize it as the default timezone, then if the user changes it, you save it to the variable. Call the variable in your logs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.