小编lip*_*kid的帖子

环境之间的时区差异

我有一个基于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 …
Run Code Online (Sandbox Code Playgroud)

java spring datetime redhat

5
推荐指数
1
解决办法
165
查看次数

标签 统计

datetime ×1

java ×1

redhat ×1

spring ×1