SpringBoot 设置时区

2022-06-27 14:54:09
## 1. 启动类上设置 ``` @SpringBootApplication public class DemoApplication { public static void main(String[] args) { TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); SpringApplication.run(DemoApplication.class, args); } @PostConstruct void setDefaultTimezone() { TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); } } ``` ## 2. JVM参数设置 ``` java -Duser.timezone=GMT+08 -jar demo.jar ``` ## 配置文件设置 application.properties ``` spring.jackson.time-zone=Asia/Shanghai spring.jackson.date-format=yyyy-MM-dd HH:mm:ss ```