我有一个基于Spring的应用程序,它托管在两个环境中:S1和S2 -RedHat服务器上的tomcat 8实例。问题是,出于日志记录和持久性目的,它感知为“现在”的时间有所不同:
环境信息:
我已经编写了简单的调试代码,不同环境下的结果有所不同:
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)