JVM 和时区
我遇到了 java 时区问题,如果有人可以帮助我。
我有一个在 tomcat 5.5 上运行的 Web 应用程序(不确定这是否相关),具有以下 JVM 版本
[someuser@webserver bin]$ java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Server VM (build 1.5.0_06-b05, mixed mode)
[someuser@webserver bin]$
系统日期是 -
[someuser@webserver bin]$ date
Mon Aug 15 09:09:46 EST 2011
在 Web 应用程序中,我在某个时间点调用 Calendar.getInstance ().getTime()
,我在日志中打印这个时间戳。
问题是这个时间戳在 EDT 中返回,尽管服务器的时间是 EST。因此,返回的日期比应有的日期晚了 1 小时。
我想要实现的是让 Calendar.getInstance().getTime()
返回与系统相同时区的日期。
我搜索了论坛,发现了一些 jvm 未正确读取系统时区的建议。 我尝试使用 -Duser.timezone=EST
参数启动 tomcat,但系统始终返回 EDT 时区的时间戳。请注意 - 尝试使用非 est 参数的 -Duser.timezone
似乎有效。 这些问题似乎具有不同的性质。
我的问题在某种程度上与这个SO问题类似。但是,我只是想获取与系统所在时区相同的日期,而不进行任何特殊处理。
你能帮忙吗?
I'm having an issue with java timezones, if anyone can help me.
I have a web application running on tomcat 5.5 (not sure if this is relevant), with the following JVM version
[someuser@webserver bin]$ java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Server VM (build 1.5.0_06-b05, mixed mode)
[someuser@webserver bin]$
The system date is, say -
[someuser@webserver bin]$ date
Mon Aug 15 09:09:46 EST 2011
Within the web application, I make a call at a certain point to Calendar.getInstance().getTime()
, and I print this timestamp in the logs.
Problem is that this timestamp gets returned in EDT, although server's time is in EST. For this reason, the date returned is 1h later than it should.
What I want to achieve is make Calendar.getInstance().getTime()
to return a date in the same timezone as the system.
I have searched the forums, and found some suggestions that the jvm is not reading correctly the system's timezone.
I have tried starting the tomcat with -Duser.timezone=EST
parameter, but the system keeps returning timestamps in the EDT timezone. Please note - trying -Duser.timezone
with a non-est parameter seems to work.
The problems seems to be of a different nature.
My issue is somehow similar with this SO question. However, I'm only trying to get the date in the same timezone as the system is in, without any special handling.
Are you able to help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我有同样的问题。对我来说,Java 正在查找
/etc/sysconfig/clock
文件,而不是/etc/localtime
文件。这是针对更多信息的评论I had the same issue. It turns out for me Java was looking in the
/etc/sysconfig/clock
file not the/etc/localtime
file. This this for comment for more infoEST 和 EDT 非常具体,其中一个总是会“错误”,具体取决于一年中的时间。尝试使用“America/New_York”时区来简单地获取“纽约现在几点”。
例如
有用的时区列表:
https://calendar.york.ac.uk/en /chcncpt.html#wp1056628
EST and EDT are very specific, and one of them will alway be "wrong" depending on the time of year. Try a timezone of "America/New_York" to get simply "what the time is in New York".
E.g.
Useful time zones list:
https://calendar.york.ac.uk/en/chcncpt.html#wp1056628
这非常简单,将其添加到您的应用程序主方法(或 servlet 上下文)中:
这将为系统中的所有日期设置时区。
This is quite simple, add this at your app main method (or servlet context):
This sets the timezone for all dates in your system.
我不确定这在 Java 中是否真的可行,但如果是这样,它肯定不是像其他人所说的那样是事实上的做事方式。请参阅此处了解 Oracle 关于此事的说明,特别是:
I'm not sure if this is actually possible in Java, but if so it's certainly not the defacto way of doing things as others have said. See here for Oracle's notes on the matter, in particular:
我在 ubuntu 服务器上遇到了同样的问题,找不到 /etc/sysconfig/clock 文件。
我解决了它使用
timedatectl
设置时区。I had the same issue on ubuntu server, and can't find /etc/sysconfig/clock file.
I solved it use the
timedatectl
to set the timezone.东部夏令时间和EST - 这是同一地理区域。但 EST 是标准时间,仅在冬季有效(在某些地方甚至在夏季),而 EDT 则对应夏令时。
您的问题可能与夏令时运动有关,所以我会深入研究这个方向。
您还可以通过设置默认时区来指定特定时区(通常采用国家/城市格式),但在这种情况下,您应该确保服务器当前时区和您指定的默认时区不会发生任何冲突。
EDT & EST - it's the same geographical zone. But EST is a standard time and it works only in winter (and in some places even in summer), and EDT is a daylight saving counterpart.
Your issue is probably related to daylight saving movements, so I'd dig into this direction.
You can also specify a particular timezone (usually in Country/City format) by setting the default timezone, but in this case you should be sure there won't be any clashes with your server current timezone and the one you specify as default.
在 Linux 上,这有点烦人,因为很长一段时间 Java(至少 Oracle/Sun Java)使用了错误的方法(好吧,我想它开始时错误较少,但情况已经改变了。)
我最好的建议是设置 TZ 环境变量,因为这是它首先要查找的内容;它看起来的其他地方(例如/etc/sysconfig/clock、/etc/localtime)有错误。我有一篇关于它的更详细的文章 http://distracted-it.blogspot.co.nz/2014/09/dont-let-java-on-linux-define-its.html,其中包括一些背景参考和验证 步。
-Duser.timezone=Pacific/Auckland
也可能有效,但当我尝试时,却没有。如果设置 TZ,您应该确保将其设置在适当的位置。例如,在 WebLogic 中间件容器中,您应该在 setDomainEnv.sh 中设置它,因为 WebLogic 将首先清理环境,并且 TZ 将不会被看到(我的文章展示了如何验证 JVM 进程是否正在看到它。)
On Linux, this is a bit annoying because for a long time Java (at least Oracle/Sun Java) used a faulty method (well, I suppose it was less faulty when it started, but things have changed.)
My best recommendation would be to set the TZ environment variable, as this is the first thing it will look for; the other placesit will look (eg. /etc/sysconfig/clock, /etc/localtime) are faulty. I have a more detailed post about it at http://distracted-it.blogspot.co.nz/2014/09/dont-let-java-on-linux-determine-its.html, which includes some background references and a verification step.
-Duser.timezone=Pacific/Auckland
may also work, but when I tried it, it didn't.If setting TZ, you should make sure you set it in an appropriate place. For example, in the WebLogic middleware container, you should set it in setDomainEnv.sh, as WebLogic will sanitize the environment first and TZ won't be seen (my post shows how you can verify that a JVM process is seeing it.)