gpt4 book ai didi

java - Spring-Boot 应用程序中未保留时区更改

转载 作者:行者123 更新时间:2023-12-05 00:53:58 25 4
gpt4 key购买 nike

当我在“Windows Server 2016 Datacenter”机器上使用 Tomcat 9 运行 Springboot 2.3.8 应用程序时,我遇到了时区问题。使用 Eclipse 或 Tomcat 9 在本地运行它不会触发问题。

我在开始时使用:

@PostConstruct
public void init()
{
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));

Calendar now = Calendar.getInstance();
TimeZone timeZone = now.getTimeZone();
System.out.println(timeZone.getDisplayName());
}

然后打印 -> 中欧标准时间

但是,稍后当我调用其中一个端点并以与以前相同的方式检查时区时

Calendar now = Calendar.getInstance();
TimeZone timeZone = now.getTimeZone();
System.out.println(timeZone.getDisplayName());

我明白了 --> 协调世界时

我假设@PostConstruct 上没有真正设置时区,或者稍后会覆盖它。

我的问题是,可能会发生什么变化以及如何解决它以使我的时区始终位于“欧洲/柏林”。

我已尝试如上所述更改应用程序内部的时区,在 tomcat 中使用 java 参数并更改机器本身的时区,但在所有情况下,更改都会被覆盖,并且在调用和端点时我会得到 UTC。

Pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<groupId>com.test</groupId>
<artifactId>project</artifactId>
<name>project</name>
<version>1.00</version>
<packaging>war</packaging>

<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- Master Versions -->
<spring.boot.version>2.3.8.RELEASE</spring.boot.version>
<oracle.version>12.2.0.1</oracle.version>
</properties>

<!-- Generates the Build Info/Properties -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Adds local jar to the final WAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>${project.basedir}/libraries/</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>

<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<!-- Spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-ldap</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-jwt</artifactId>
<version>1.1.1.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.5.0.RELEASE</version>
</dependency>

<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
</dependency>

<!-- javax -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>

<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
</dependency>

<!-- jjwt -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>

<!-- Oracle JDBC driver -->
<dependency>
<groupId>com.oracle.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>${oracle.version}</version>
</dependency>

<!-- HikariCP connection pool -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>

<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-example -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.tika/tika-parsers -->
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.25</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<!-- Spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>

<!-- Mockito -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</dependency>

<!-- Powermock -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>

最佳答案

您正在使用糟糕的日期时间类,这些类在几年前已被现代 java.time 类所取代。 切勿使用 CalendarDateTimeZoneSimpleDateFormat

你问:

what could be doing the change

作为 commented by Ole V.V. ,您的 JVM 当前的默认时区可能会被其他一些 Java 代码覆盖。 JVM 内任何应用程序的任何线程中的任何 Java 代码都可以随时更改 JVM 的当前默认时区。这样的更改会立即影响在该 JVM 中运行的所有应用程序和线程。因此,这有效地使当前默认时区成为不可靠的工具。

你问:

how could I fix it to have my timezone always at "Europe/Berlin".

始终通过传递 ZoneId 明确指定您的时区 java.time 方法的对象。

不要依赖默认时区,而是始终将所需/预期的时区指定为 java.time 中各种方法的可选参数。

而不是这个:

ZonedDateTime now = ZonedDateTime.now() ;  // Do NOT do this. Do not rely implicitly on the JVM’s current default time zone.

……这样做:

ZoneId z = ZoneId.of( "Europe/Berlin" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ; // Do this. Always pass the desired/expected time zone explicitly.

在标准 ISO 8601 中生成文本格式扩展以在方括号中附加时区名称,调用 toString

String output = now.toString() ;

要生成其他格式的文本,让java.time自动本地化。

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
String output = now.format( f ) ;

提示:在服务器上,通常最好将主机操作系统和 JVM 的默认时区设置为 UTC,偏移量为零时分秒。

提示:通常最好在 UTC 中完成业务逻辑、调试、日志记录和系统管理员工作。时区只能用于 (a) 向用户展示,以及 (b) 业务规则需要的地方。

Table of date-time types in Java, both modern and legacy


关于java.time

java.time框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy日期时间类,例如 java.util.Date , Calendar , & SimpleDateFormat .

要了解更多信息,请参阅 Oracle Tutorial .并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310 .

Joda-Time项目,现在在 maintenance mode , 建议迁移到 java.time类。

您可以直接与您的数据库交换 java.time 对象。使用 JDBC driver符合 JDBC 4.2或更晚。不需要字符串,不需要 java.sql.* 类。 Hibernate 5 & JPA 2.2 支持 java.time

从哪里获取 java.time 类?

关于java - Spring-Boot 应用程序中未保留时区更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66855677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com