簡體   English   中英

如何強制 Spring Boot JVM 進入 UTC 時區?

[英]How do I force a Spring Boot JVM into UTC time zone?

我將Force 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 中:

     <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"

但它打印出來

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

我認為您可以在應用程序級別設置應用程序的時區。 我認為這個鏈接會對你有所幫助。 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"));
    }

}

希望這可以幫到你!

如果要將 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

您可以使用帶有@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());
    }
}

原文

如果您的應用程序在 linux 下運行,則提供更多選項:

設置TZ環境變量

請參閱“在 POSIX 系統中,用戶可以通過 TZ 環境變量指定時區”( https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html )。

此選項在任何雲環境中都特別有用。

符號鏈接/etc/locatime

即在我的本地系統/etc/locatime符號鏈接到/usr/share/zoneinfo/Europe/Berlin

➜ ls -l /etc/localtime  
lrwxrwxrwx 1 root root 33 22. Jan 23:01 /etc/localtime -> /usr/share/zoneinfo/Europe/Berlin

您可以使用ln -s /usr/share/zoneinfo/GMT /etc/localtime輕松更改符號鏈接,可能的值可以在/usr/share/zoneinfo/中找到。

通過掛載主機卷,此選項也可以在許多雲環境中使用,請參閱帶有命令和參數的 POD 中的 kubernetes 時區

那么,基於你的問題,你有兩個選擇:

  1. 在參數行user.timezone的pom.xml設置中,在surefire設置systemPropertyVariables中的屬性之前:

     <plugin> ... <configuration> <argLine>-Duser.timezone=UTC</argLine> </configuration> ... </plugin> 
  2. 在Application類或某個Configuration類中創建init方法,並在方法setDefault時區內創建為UTC:

     @PostConstruct public void init() { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); // OR you can use the line below // System.setProperty("user.timezone", "UTC") } 

后構造有時不起作用,使其預構造對我有用

@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);
    }

    
}

當我運行單元測試時,這對我來說就像在 intellij 上的魅力:你必須在 junit 或 maven 配置的 vm 參數 GMT 上添加下面的命令(我不知道為什么 UTC 對我不起作用,希望這對你有幫助^^)

    -Duser.timezone="GMT+2"

<properties> <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments> </properties>這對我不起作用。

但是,如果您使用jvmArguments代替,那對我有用。 參考: https ://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/ ...

只需<configuration> <jvmArguments>-Duser.timezone=UTC</jvmArguments> </configuration>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM