Java时区搞砸了 [英] Java Time Zone is messed up

查看:82
本文介绍了Java时区搞砸了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Tomcat应用程序,我需要显示一些时间值。不幸的是,时间过了一个小时。我调查了一下,发现我的默认TimeZone被设置为:

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 default TimeZone is being set to:

sun.util.calendar.ZoneInfo[id="GMT-08:00",
                           offset=-28800000,
                           dstSavings=0,
                           useDaylight=false,
                           transitions=0,
                           lastRule=null]

而不是太平洋时区。当我尝试打印默认时区时,会进一步指示显示名称,它出现GMT-08:00,这似乎表明它没有正确设置到美国太平洋时区。我在Ubuntu Hardy Heron上运行,从Gutsy Gibbon升级。

Rather than the Pacific time zone. This is further indicated when I try to print the default time zone's display name, and it comes up "GMT-08:00", which seems to indicate to me that it is not correctly set to the US Pacific time zone. I am running on Ubuntu Hardy Heron, upgraded from Gutsy Gibbon.

是否有一个配置文件我可以更新告诉JRE使用Pacific以及所有相关的夏令时信息?我的机器上的时间显示正确,所以它似乎不是操作系统范围内的错误配置。

Is there a configuration file I can update to tell the JRE to use Pacific with all the associated daylight savings time information? The time on my machine shows correctly, so it doesn't seem to be an OS-wide misconfiguration.

好的,这是一个更新。一位同事建议我在我的/ etc / profile中更新JAVA_OPTS以包含-Duser.timezone = US / Pacific,这有效(我也看到了CATALINA_OPTS,我也更新了)。实际上,我只是将更改导出到变量而不是使用新的/ etc / profile(稍后重新启动将获取更改,我将是金色的)。

Ok, here's an update. A coworker suggested I update JAVA_OPTS in my /etc/profile to include "-Duser.timezone=US/Pacific", which worked (I also saw CATALINA_OPTS, which I updated as well). Actually, I just exported the change into the variables rather than use the new /etc/profile (a reboot later will pick up the changes and I will be golden).

但是,我仍然认为有一个更好的解决方案......应该有一个Java配置,它说明它正在使用什么时区,或者它是如何抓住时区的。如果有人知道这样的设置,那就太棒了,但是现在这是一个不错的解决方法。

However, I still think there is a better solution... there should be a configuration for Java somewhere that says what timezone it is using, or how it is grabbing the timezone. If someone knows such a setting, that would be awesome, but for now this is a decent workaround.

我正在使用1.5,这绝对是一个DST问题。如您所见,时区设置为不使用夏令时。我的信念是它通常设置为-8偏移而不是特定的太平洋时区。由于泛型-8偏移没有夏令时信息,它当然不使用它,但问题是,我在哪里告诉Java在启动时使用太平洋时区?我不是在寻找一个程序化的解决方案,它应该是一个配置解决方案。

I am using 1.5, and it is most definitely a DST problem. As you can see, the time zone is set to not use daylight savings. My belief is it is generically set to -8 offset rather than the specific Pacific timezone. Since the generic -8 offset has no daylight savings info, it's of course not using it, but the question is, where do I tell Java to use Pacific time zone when it starts up? I'm NOT looking for a programmatic solution, it should be a configuration solution.

推荐答案

这是一个怪癖的方式JVM查找zoneinfo文件。请参阅错误ID 6456628

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

最简单的解决方法是使/ etc / localtime成为正确的zoneinfo文件的符号链接。对于太平洋时间,以下命令应该有效:

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没有任何问题与符号链接方法。

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

编辑:为命令添加sudo。

Added "sudo" to the commands.

这篇关于Java时区搞砸了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆