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

Chl*_*loe 4 java timezone spring 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>
    
    Run Code Online (Sandbox Code Playgroud)
  • mvn spring-boot:运行-Dspring-boot.run.jvmArguments =“-Duser.timezone = UTC”

但它打印出来

System.out.println(TimeZone.getDefault());
Run Code Online (Sandbox Code Playgroud)

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

Kar*_*cki 24

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

<properties>
  <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments>
</properties>
Run Code Online (Sandbox Code Playgroud)

这相当于命令行语法:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"
Run Code Online (Sandbox Code Playgroud)

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

java -Duser.timezone=UTC -jar app.jar
Run Code Online (Sandbox Code Playgroud)

  • @KarolDowbecki,是否可以选择将其放入 application.properties 文件中? (5认同)
  • 第一个选项`&lt;properties` 不起作用。它仍然打印 `id="America/New_York"`。第二个选项`-Dspring-boot.run.jvmArguments...` 也不起作用。 (3认同)

Jab*_*ash 19

您可以使用带有@Configuration注释的类来配置时区。你可以把它放在你项目的任何地方。我通常将所有适合此类别的类放在一个名为config. 确保将@PostConstruct注释添加到实际设置时区的方法中。

import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class LocaleConfig {

    @PostConstruct
    public void init() {

        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

        System.out.println("Date in UTC: " + new Date().toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

原文


小智 9

我认为您可以在应用程序级别设置应用程序的时区。我认为此链接将为您提供帮助。 https://www.onlinetutorialspoint.com/spring-boot/how-to-set-spring-boot-settimezone.html

因此,您需要做的是向“ @SpringBootApplication”注释所在的主类中添加“ @PostConstruct”注释,然后在其中添加时区设置方法。这是一个例子。

@SpringBootApplication
public class HellotimezoneApplication {

    public static void main(String[] args) {
        SpringApplication.run(HellotimezoneApplication.class, args);
    }

    @PostConstruct
    public void init(){
      // Setting Spring Boot SetTimeZone
      TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    }

}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮到你!

  • 这不适用于在调用 `init()` 方法之前运行的代码,例如 JVM 初始化或 Spring bean 构造。如果有的话,在“main()”方法中执行此操作会更谨慎,但万无一失的方法是使用“-D”选项。 (7认同)

Nav*_*hna 6

后置构造有时不起作用,而预置构造对我有用

@EnableSwagger2
@SpringBootApplication(scanBasePackages = { "com.app" })
public class Application {
    
    public static void main(String[] args) {
        System.out.println("Setting the timezone"+TimeZone.getTimeZone("GMT+9:00").getID());
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+9:00"));
        SpringApplication.run(Application.class, args);
    }

    
}
Run Code Online (Sandbox Code Playgroud)