Java Time Zone is messed up

前端 未结 4 1527
悲哀的现实
悲哀的现实 2020-11-28 06:52

I am running a Tomcat application, and I need to display some time values. Unfortunately, the time is coming up an hour off. I looked into it and discovered that my defaul

相关标签:
4条回答
  • 2020-11-28 07:07

    I had a similar issue, possibly the same one. However my tomcat server runs on a windows box so the symlink solution will not work.

    I set -Duser.timezone=Australia/Sydney in the JAVA_OPTS however tomcat would not recognize that DST was in effect. As a workaround I changed Australia/Sydney (GMT+10:00) to Pacific/Numea (GMT+11:00) so that times would correctly display however I would love to know the actual solution or bug, if any.

    0 讨论(0)
  • 2020-11-28 07:12

    On Ubuntu, it's not enough to just change the /etc/localtime file. It seems to read /etc/timezone file, too. It's better follow the instruction to set the time zone properly. In particular, do the following:

    $ sudo cp /etc/timezone /etc/timezone.dist
    $ echo "Australia/Adelaide" | sudo tee /etc/timezone
    Australia/Adelaide
    $ sudo dpkg-reconfigure --frontend noninteractive tzdata
    
    Current default time zone: 'Australia/Adelaide'
    Local time is now:      Sat May  8 21:19:24 CST 2010.
    Universal Time is now:  Sat May  8 11:49:24 UTC 2010.
    

    On my Ubuntu, if /etc/localtime and /etc/timezone are inconsistent, Java seems to read default time zone from /etc/timezone .

    0 讨论(0)
  • 2020-11-28 07:22

    It's a "quirk" in the way the JVM looks up the zoneinfo file. See Bug ID 6456628.

    The easiest workaround is to make /etc/localtime a symlink to the correct zoneinfo file. For Pacific time, the following commands should work:

    # sudo cp /etc/localtime /etc/localtime.dist
    # sudo ln -fs /usr/share/zoneinfo/America/Los_Angeles /etc/localtime
    

    I haven't had any problems with the symlink approach.

    Edit: Added "sudo" to the commands.

    0 讨论(0)
  • 2020-11-28 07:23

    It may help to double-check the timezone rules your OS is using.

    /usr/bin/zdump -v /etc/localtime | less
    

    This file should contain your daylight savings rules, like this one for the year 2080:

    /etc/localtime  Sun Mar 31 01:00:00 2080 UTC = Sun Mar 31 02:00:00 2080 BST isdst=1 gmtoff=3600
    

    You can compare this with the timezone rules you think you should be using. They can be found in /usr/share/zoneinfo/.

    0 讨论(0)
提交回复
热议问题