![](/img/trans.png)
[英]How do I indicate to Spring Boot to use UTC time zone for date parameters?
[英]How do I force a Spring Boot JVM into UTC time zone?
我试过了
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>
但它打印出来
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 时区。
那么,基于你的问题,你有两个选择:
在参数行user.timezone的pom.xml设置中,在surefire设置systemPropertyVariables中的属性之前:
<plugin> ... <configuration> <argLine>-Duser.timezone=UTC</argLine> </configuration> ... </plugin>
在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.