时区更改不会保留在 Spring-Boot 应用程序中

ur3*_*r3k 3 java timezone spring-mvc spring-boot

当我在“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());
    }
Run Code Online (Sandbox Code Playgroud)

打印 ->中欧标准时间

但是,稍后当我调用其中一个端点时,我会像以前一样检查时区

Calendar now = Calendar.getInstance();
TimeZone timeZone = now.getTimeZone();
System.out.println(timeZone.getDisplayName());
Run Code Online (Sandbox Code Playgroud)

我得到 -->协调世界时

我假设时区没有在 @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>
Run Code Online (Sandbox Code Playgroud)

Bas*_*que 6

您正在使用可怕的日期时间类,这些类多年前已被现代java.time类取代。切勿使用Calendar、、、DateTimeZoneSimpleDateFormat

\n

你问:

\n
\n

可能会发生什么改变

\n
\n

正如Ole VV 所评论的,您的 JVM\xe2\x80\x99s 当前默认时区可能会被其他一些 Java 代码覆盖。JVM 内任何应用程序的任何线程中的任何 Java 代码都可以随时更改 JVM\xe2\x80\x99s 当前的默认时区。此类更改会立即影响该 JVM 中运行的所有应用程序和线程。因此,这实际上使当前的默认时区成为一个不可靠的工具。

\n

你问:

\n
\n

我该如何修复它以使我的时区始终位于“欧洲/柏林”。

\n
\n

始终通过将对象传递给java.time方法来显式指定您的时区ZoneId

\n

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

\n

而不是这个:

\n
ZonedDateTime now = ZonedDateTime.now() ;  // Do NOT do this. Do not rely implicitly on the JVM\xe2\x80\x99s current default time zone.\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa6 这样做:

\n
ZoneId z = ZoneId.of( "Europe/Berlin" ) ;\nZonedDateTime now = ZonedDateTime.now( z ) ;  // Do this. Always pass the desired/expected time zone explicitly. \n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
String output = now.toString() ;\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;\nString output = now.format( f ) ;\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

\n

Java 中的日期时间类型表(现代和传统)

\n
\n

关于java.time

\n

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

\n

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

\n

Joda -Time项目现在处于维护模式,建议迁移到java.time类。

\n

您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC 驱动程序。不需要字符串,不需要类。Hibernate 5 和 JPA 2.2 支持java.timejava.sql.*

\n

从哪里获取 java.time 类?

\n\n