[英]How do I force a Spring Boot JVM into UTC time zone?
I saw Force Java timezone as GMT/UTC我将Force Java 时区视为 GMT/UTC
I tried我试过了
user.timezone=UTC
in config/application.properties
config/application.properties
中的user.timezone=UTC
user.timezone=GMT
In the pom.xml:在 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>
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
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.所以你需要做的是在“@SpringBootApplication”注解所在的主类中添加“@PostConstruct”注解,并在那里添加时区设置方法。 Here is an example.
这是一个例子。
@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!希望这可以帮到你!
Use spring-boot.run.jvmArguments
property if 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
You can configure the timezone with a class annotated with the @Configuration
annotation.您可以使用带有
@Configuration
注释的类来配置时区。 You can put it anywhere in your project.您可以将它放在项目中的任何位置。 I typically house all classes that fit under this category in a package called
config
.我通常将所有适合此类别的类都放在一个名为
config
的包中。 Make sure you add the @PostConstruct
annotation to the method that is actually setting the timezone.确保将
@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见原文
More options in case your application is running under linux:如果您的应用程序在 linux 下运行,则提供更多选项:
TZ
environment variableTZ
环境变量See "In POSIX systems, a user can specify the time zone by means of the TZ environment variable" ( https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html ).请参阅“在 POSIX 系统中,用户可以通过 TZ 环境变量指定时区”( https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html )。
This option is especially useful in any cloud environment.此选项在任何云环境中都特别有用。
/etc/locatime
/etc/locatime
Ie in my local system /etc/locatime
symlinks to /usr/share/zoneinfo/Europe/Berlin
:即在我的本地系统
/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
You can easily change the symbolic link with ln -s /usr/share/zoneinfo/GMT /etc/localtime
, the possible values can be found within /usr/share/zoneinfo/
.您可以使用
ln -s /usr/share/zoneinfo/GMT /etc/localtime
轻松更改符号链接,可能的值可以在/usr/share/zoneinfo/
中找到。
This option can also be used in many cloud environments by mounting a hostvolume, see kubernetes timezone in POD with command and argument .通过挂载主机卷,此选项也可以在许多云环境中使用,请参阅带有命令和参数的 POD 中的 kubernetes 时区。
Well, based your question you have two options: 那么,基于你的问题,你有两个选择:
In the pom.xml setting by argument line user.timezone, before surefire sets the properties in systemPropertyVariables: 在参数行user.timezone的pom.xml设置中,在surefire设置systemPropertyVariables中的属性之前:
<plugin> ... <configuration> <argLine>-Duser.timezone=UTC</argLine> </configuration> ... </plugin>
Creating a init method in your Application class or some Configuration class and within the method setDefault Time Zone to UTC: 在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") }
Post constructs don't work sometimes making it pre construct worked for me后构造有时不起作用,使其预构造对我有用
@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>
That did not work for me. <properties> <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments> </properties>
这对我不起作用。
But if you use jvmArguments instead that worked for me.但是,如果您使用jvmArguments代替,那对我有用。 Reference: https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/ …
参考: https ://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/ ...
simply <configuration> <jvmArguments>-Duser.timezone=UTC</jvmArguments> </configuration>
只需
<configuration> <jvmArguments>-Duser.timezone=UTC</jvmArguments> </configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.