在不重新启动服务器的情况下处理Tomcat中的夏令时 [英] Handle Daylight Saving in Tomcat without server restart

查看:192
本文介绍了在不重新启动服务器的情况下处理Tomcat中的夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Tomcat上运行的Java Web应用程序,用于为客户端创建计划的事件。我对基础操作系统,Tomcat和JVM的默认日期时间有疑问。

I have a Java web application running on Tomcat to create scheduled events for a client. I have a question regarding the default date time for underlying operating system, Tomcat and JVM.

当我检索 Date 通过Java代码,它类似于基础操作系统。然后,我只是为了模拟夏时制效果而更改了操作系统时间,但是该应用程序没有反映出操作系统时间。

When I retrieve the Date through the Java code it is similar to the underlying operating system. Then I changed the operating system time just to simulate the Daylight Saving effect, but the application does not reflect the operating system time.

然后,我对其进行了更多了解,发现JRE负责维护JVM的日期时间。但是,当我重新启动tomcat时,它开始反映操作系统时间。

Then I read more about it and found that the JRE is responsible for maintaining the Date Time for the JVM. However when I reboot the tomcat it starts reflecting the operating system time.

有人可以解释一下其背后的理论吗?根据我的阅读,我们可以使用TZupdater更新JRE的最新日期时间更改,并且系统将处理夏时制,而无需重新启动tomcat。

Can any one please explain the theory behind this? As my readings we can use TZupdater to update the latest date time changes for the JRE and the system will handle the Daylight Saving without the restart of the tomcat.

推荐答案

此处涉及不同的设置:


  • 系统不仅具有当前时间,还具有时区

  • 运行Tomcat的JVM也具有时区设置。

时间操作系统和JVM的区域设置为您照顾了夏季时间(夏令时,DST)。因此,设置系统时间以反映DST是不正确的方法。相反,您应该将操作系统时区设置为所需的时区,并且操作系统将自动调整DST在春季开始和秋季在结束时显示的时间。

The time zone settings of the operating system and of the JVM are taking care of summer time (daylight saving time, DST) for you. So setting the system time to reflect DST is the incorrect way. Instead you should set the operating system time zone to the desired time zone, and the operating system will automatically adjust the time it displays when DST begins in spring and when it ends in autumn.

您真的不应该使用 Date 来表示日期和时间。该类早已过时,并且Java.time(现代Java日期和时间API)的类要好用得多。无论如何,您的日期都不包含您所在时区的时间。相反,它表示时间线上的一个点,与时区无关。令人困惑的是,当您打印日期时,隐式调用其 toString 方法,然后该 toString 方法抢占了日期。 JVM的时区设置,并将其用于生成字符串。这可能会给您一个错误的印象,即 Date 中有一个时区。

You really shouldn’t use Date for representing date and time. That class is long outdated, and the classes of java.time, the modern Java date and time API, are much nicer to work with. If you do anyway, your Date does not contain the time in your time zone. Instead it represents a point on the time line independent of time zones. What confuses many is that when you print the date, thereby implicitly calling its toString method, then that toString method grabs the time zone setting of your JVM and uses it for generating the string. This may give you the false impression that the Date has a time zone in it.

现在我提到了JVM时区设置。除非进行任何操作以获得其他东西,否则在启动JVM时会将JVM时区设置初始化为操作时区设置,但是即使您更改了操作系统设置也不会更改。另一方面,可以从在JVM中运行的Java程序(包括在Tomcat服务器中运行的任何程序)更改JVM设置。

Now I mentioned the JVM time zone setting. Unless something is done to obtain something else, the JVM time zone setting is initialized to the operating time zone setting when the JVM is launched, but is not changed even if you change the operating system setting. On the other hand the JVM setting may be changed from a Java program running in the JVM (this includes any program running in your Tomcat server).

通常,最好的方法是在Java中获取当前时间是您自己指定时区。例如:

Generally the best way to get the current time in Java is to specify time zone yourself. For example:

    System.out.println(ZonedDateTime.now(ZoneId.of("Pacific/Noumea")));

这刚刚打印


2018-03-06T06:20:23.903292 + 11:00 [Pacific / Noumea]

2018-03-06T06:20:23.903292+11:00[Pacific/Noumea]

发生了什么事从操作系统时钟中获取当前时间,并将其转换为指定时区中的当前时间,并考虑到DST是否在每年的这个时间生效。与 Date ZonedDateTime 相反,日期和时间是,而是时区。 ZonedDateTime 是我提到的现代API java.time中的类之一。

What happened was that Java took the current time from the operating system clock and translated it to the current time in the specified time zone, taking DST into account if DST is in effect at this time of year. Contrary to a Date a ZonedDateTime is date and time and a time zone. ZonedDateTime is one of the classes from java.time, the modern API I mentioned.

通过指定显式时间区域中,您不受任何可能会意外更改的设置的影响。而且,依靠所需的时区,您无需在DST开始和结束时更改系统时钟。

By specifying an explicit time zone you are independent of any setting that might have been changed unpredictably. And by relying on your desired time zone you are free from changing the system clock when DST begins and ends.

是的,您是正确的,可能需要使用TZupdater以确保您的JVM包含正确的信息,例如有关您所在时区的DST。政客有时会在短时间内更改DST规则,并且世界上Java的安装不会在这种情况下自动更改。升级Java版本时,通常还会获得最新的时区信息。但是,如果您使用的不是最新的Java版本,则TZupdater是确保您的时区信息是最新的方法。

And yes, you are correct that you may need to use the TZupdater to make sure that your JVM contains the correct information, for example about DST for your time zone. Politicians sometimes change the DST rules on a short notice, and the world’s Java installations don’t change automatically when that happens. When you upgrade your Java version you will typically also get the latest time zone information. But if you are using a less-than-brand-new Java version, TZupdater is the way to make sure your time zone information is current.

链接: 有关java.util.Date的全部信息

这篇关于在不重新启动服务器的情况下处理Tomcat中的夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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