如何强制 Spring Boot JVM 进入 UTC 时区?

2022-01-16 00:00:00 timezone spring java spring-boot

我看到 强制 Java 时区为 GMT/UTC

我试过了

  • mvn spring-boot:run -Dexec.args="-Duser.timezone=GMT"
  • mvn spring-boot:run -Dexec.args="-Duser.timezone=UTC"
  • config/application.properties
  • 中的user.timezone=UTC
  • user.timezone=GMT
  • 在 pom.xml 中:

     <插件><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><配置><属性><spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments></属性></配置></插件>

  • mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"

但它会打印出来

System.out.println(TimeZone.getDefault());

<块引用>

sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

Spring Boot 1.5.19,Java 8

解决方案

使用 spring-boot.run.jvmArguments 属性 如果您想将 JVM 选项从 Maven Spring Boot 插件传递到分叉的 Spring Boot 应用程序:

<spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments></属性>

这相当于命令行语法:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"

或在运行完全打包的 Spring Boot 应用程序时:

java -Duser.timezone=UTC -jar app.jar

I saw Force Java timezone as GMT/UTC

I tried

  • mvn spring-boot:run -Dexec.args="-Duser.timezone=GMT"
  • mvn spring-boot:run -Dexec.args="-Duser.timezone=UTC"
  • user.timezone=UTC in config/application.properties
  • user.timezone=GMT
  • In the pom.xml:

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <properties>
                  <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments>
                </properties>
            </configuration>
        </plugin>
    

  • mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"

But it prints out

System.out.println(TimeZone.getDefault());

sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

Spring Boot 1.5.19, Java 8

解决方案

Use spring-boot.run.jvmArguments property if you want to pass JVM options from Maven Spring Boot Plugin to forked Spring Boot application:

<properties>
  <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments>
</properties>

This is be equivalent to command line syntax:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"

or when running a fully packaged Spring Boot application:

java -Duser.timezone=UTC -jar app.jar

相关文章