Chl*_*loe 4 java timezone spring spring-boot
我试过了
user.timezone=UTC 在 config/application.propertiesuser.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>
Run Code Online (Sandbox Code Playgroud)但它打印出来
System.out.println(TimeZone.getDefault());
Run Code Online (Sandbox Code Playgroud)
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
Kar*_*cki 24
如果要将 JVM 选项从 Maven Spring Boot 插件传递到分叉的 Spring Boot 应用程序,请使用spring-boot.run.jvmArguments属性:
<properties>
<spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments>
</properties>
Run Code Online (Sandbox Code Playgroud)
这相当于命令行语法:
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"
Run Code Online (Sandbox Code Playgroud)
或者在运行完全打包的 Spring Boot 应用程序时:
java -Duser.timezone=UTC -jar app.jar
Run Code Online (Sandbox Code Playgroud)
Jab*_*ash 19
您可以使用带有@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());
}
}
Run Code Online (Sandbox Code Playgroud)
见原文
小智 9
我认为您可以在应用程序级别设置应用程序的时区。我认为此链接将为您提供帮助。 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"));
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮到你!
后置构造有时不起作用,而预置构造对我有用
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10805 次 |
| 最近记录: |