一尘不染

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

spring-boot

我看到Java时区为GMT / UTC

我试过了

  • mvn spring-boot:run -Dexec.args =“-Duser.timezone = GMT”
  • mvn spring-boot:run -Dexec.args =“-Duser.timezone = UTC”
  • user.timezone=UTCconfig/application.properties
  • user.timezone=GMT
  • 在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:运行-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


阅读 693

收藏
2020-05-30

共1个答案

一尘不染

如果要将JVM选项从Maven Spring Boot插件传递到派生的Spring Boot应用程序,请使用spring- boot.run.jvmArguments属性

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

这等效于命令行语法:

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

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

java -Duser.timezone=UTC -jar app.jar
2020-05-30