Java 如何强制 Spring Boot JVM 进入 UTC 时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54316667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force a Spring Boot JVM into UTC time zone?
提问by Chloe
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
inconfig/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"
- mvn spring-boot:run -Dexec.args="-Duser.timezone=GMT"
- mvn spring-boot:run -Dexec.args="-Duser.timezone=UTC"
user.timezone=UTC
在config/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"
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]]
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 1.5.19,Java 8
采纳答案by Karol Dowbecki
Use spring-boot.run.jvmArguments
propertyif you want to pass JVM options from Maven Spring Boot Plugin to forked Spring Boot application:
如果要将 JVM 选项从 Maven Spring Boot 插件传递到分叉的 Spring Boot 应用程序,请使用spring-boot.run.jvmArguments
属性:
<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:
或者在运行完全打包的 Spring Boot 应用程序时:
java -Duser.timezone=UTC -jar app.jar
回答by Jabari Dash
You can configure the timezone with a class annotated with the @Configuration
annotation. You can put it anywhere in your project. I typically house all classes that fit under this category in a package called config
. Make sure you add the @PostConstruct
annotation to the method that is actually setting the timezone.
您可以使用带有@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());
}
}
See the original article
见原文
回答by YONGSOO KIM
I think you can set your application's timezone on your application level. I think this link will help you. https://www.onlinetutorialspoint.com/spring-boot/how-to-set-spring-boot-settimezone.html
我认为您可以在应用程序级别设置应用程序的时区。我认为这个链接会对你有所帮助。 https://www.onlinetutorialspoint.com/spring-boot/how-to-set-spring-boot-settimezone.html
So What you need to do is adding "@PostConstruct" annotation to the main class where "@SpringBootApplication" annotation is located, and add timezone setting method there. Here is an example.
那么你需要做的是在“@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"));
}
}
Hope this can help you!
希望这可以帮到你!