【问题标题】:How do I force a Spring Boot JVM into UTC time zone?如何强制 Spring Boot JVM 进入 UTC 时区?
【发布时间】:2019-06-16 10:03:30
【问题描述】:

我看到Force Java timezone as 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: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 v1:mvn spring-boot:run -Drun.jvmArguments="-Duser.timezone=UTC"

标签: java spring spring-boot timezone


【解决方案1】:

后构造有时不起作用,因此前构造对我有用

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

    
}

【讨论】:

    【解决方案2】:

    &lt;properties&gt; &lt;spring-boot.run.jvmArguments&gt;-Duser.timezone=UTC&lt;/spring-boot.run.jvmArguments&gt; &lt;/properties&gt; 这对我不起作用。

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

    只需 &lt;configuration&gt; &lt;jvmArguments&gt;-Duser.timezone=UTC&lt;/jvmArguments&gt; &lt;/configuration&gt;

    【讨论】:

      【解决方案3】:

      更多选项,以防您的应用程序在 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/ 中找到。

      此选项也可以通过挂载主机卷在许多云环境中使用,请参阅kubernetes timezone in POD with command and argument

      【讨论】:

        【解决方案4】:

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

        original article

        【讨论】:

          【解决方案5】:

          我认为您可以在应用程序级别设置应用程序的时区。我认为这个链接会对你有所帮助。 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"));
              }
          
          }
          

          希望对你有帮助!

          【讨论】:

          • 这不适用于在调用 init() 方法之前运行的代码,例如JVM 初始化或 Spring bean 构造。如果有的话,在main() 方法中这样做会更谨慎,但万无一失的方法是使用-D 选项。
          【解决方案6】:

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

          <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
          

          【讨论】:

          • 第一个选项&lt;properties 不起作用。它仍然打印id="America/New_York"。第二个选项-Dspring-boot.run.jvmArguments... 也不起作用。
          • @Chloe 使用 -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"-Dspring-boot.run.jvmArguments="-Duser.timezone=America/New_York 为我工作,使用 Spring Boot 2.1.2.RELEASE 和 Java 11。使用 start.spring.io 生成的项目。
          • 我明白了。必须是 v1 中的错误并在 v2 中修复。 java -Duser.timezone=UTC -jar target\App-SNAPSHOT.war 确实工作。
          • -Duser.timezone=UTC 不起作用,但如果你使用 jvmArguments这对我有用。参考:docs.spring.io/spring-boot/docs/2.2.2.RELEASE/maven-plugin/… 就这么简单:-Duser.timezone=UTC
          • @KarolDowbecki,是否可以选择将其放入 application.properties 文件中?
          猜你喜欢
          • 2017-05-27
          • 2011-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多