环境之间的时区差异

lip*_*kid 5 java spring datetime redhat

我有一个基于Spring的应用程序,它托管在两个环境中:S1S2 -RedHat服务器上的tomcat 8实例。问题是,出于日志记录和持久性目的,它感知为“现在”的时间有所不同:

  • 它报告S1的正确时间
  • S2上一小时

环境信息:

  • 红帽企业Linux服务器6.9
  • JVM版本:1.8
  • -Duser.timezone =欧洲/柏林
  • 两种环境的/ etc / localtime相同

我已经编写了简单的调试代码,不同环境下的结果有所不同:

System.out.println("[TimeZone]TimeZone ID: " + TimeZone.getDefault().getID());
System.out.println("[TimeZone]TimeZone name: " + TimeZone.getDefault().getDisplayName());

Date date = new Date();
LocalDateTime localDate = LocalDateTime.now();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

System.out.println("[Date]Date and time: " + df.format(date));
System.out.println("[LocalDateTime]Date and time: " + formatter.format(localDate));

df.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
ZonedDateTime zdt = localDate.atZone(ZoneId.of("Europe/Berlin"));

System.out.println("[Date]Date and time in Berlin: " + df.format(date));
System.out.println("[ZonedDateTime]Date and time in Berlin: " + formatter.format(zdt));
Run Code Online (Sandbox Code Playgroud)

结果:

S1

[TimeZone]TimeZone ID: Europe/Berlin
[TimeZone]TimeZone name: Central European Time
[Date]Date and time: 2018-07-10 14:24:52
[LocalDateTime]Date and time: 2018-07-10 14:24:52
[Date]Date and time in Berlin: 2018-07-10 14:24:52
[ZonedDateTime]Date and time in Berlin: 2018-07-10 14:24:52
Run Code Online (Sandbox Code Playgroud)

S2

[TimeZone]TimeZone ID: GMT+01:00
[TimeZone]TimeZone name: GMT+01:00
[Date]Date and time: 2018-07-10 13:29:18
[LocalDateTime]Date and time: 2018-07-10 13:29:18
[Date]Date and time in Berlin: 2018-07-10 14:29:18
[ZonedDateTime]Date and time in Berlin: 2018-07-10 13:29:18
Run Code Online (Sandbox Code Playgroud)

一些对角线信息:

xxx@serv1:~ > ls -l /etc/localtime
-rw-r--r--. 1 root root 2309 Apr  8  2016 /etc/localtime
xxx@serv1:~ > zdump /etc/sysconfig/clock
/etc/sysconfig/clock  Fri Jul 20 09:21:01 2018

xxx@serv2:~ > ls -l /etc/localtime
-rw-r--r--. 1 root root 2309 Feb 13  2014 /etc/localtime
xxx@serv2:~ > zdump /etc/sysconfig/clock
/etc/sysconfig/clock  Fri Jul 20 09:20:47 2018
Run Code Online (Sandbox Code Playgroud)

您可能已经注意到,尽管设置了JVM属性,但TimeZone并未为S2上的应用正确加载。

所以问题是,我该如何纠正S2上应用程序的时间?或者至少,我该如何进一步调查?

Pow*_*tat 0

看起来是一个简单的问题:

S1上,您已将Europe/Berlin设置为时区,而在S2上,您已设置GMT+01:00

您还告知我们您的实际日期是 2018 年7 月 10 日,属于德国夏季时间(从 S1 时区欧洲/柏林提取)。

因此,在S1上,欧洲/柏林时区根据夏令时(夏令时)在 GMT+01 和 GMT+02 之间变化 - 而在S2上,它始终是 GMT+01。

这解释了 S1 和 S2 之间 1 小时的差异。

要解决此问题,您最好将S2上的时区也更改为欧洲/柏林,以便在那里也可以使用自动夏令时。

1 小时的休息时间只能在夏季检测到,而在正常(通常被错误地称为“冬季”)时间内检测不到。