Giuseppe
Giuseppe

Reputation: 1

Timezone Springboot Tomcat 8 AS

On my spring boot app running on Tomcat 8 i have this code into SpringBootServletInitializer:

public class MyApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MyApplication.class);
}

@PostConstruct
public void init() {
    // Setting Spring Boot SetTimeZone
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
} }

On my tomcat application server, i have some war write with spring framework, the problem is that all application inside my tomcat has been changed timezone according to my app, and this is wrong.

How can i set the timezone only for my application, without affecting the timezone of the other apps into tomcat?

Thanks

Upvotes: 0

Views: 314

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 339043

The JVM running your Tomcat server has only one default time zone. Changing that default immediately affects all code in all threads of all apps running within that JVM. Therefore calling TimeZone.setDefault is almost never the right thing to do.

Learn about the modern java.time classes that years ago supplanted the terrible legacy date-time classes. Specifically, use ZoneId & ZoneOffset rather than TimeZone. For UTC, use the constant ZoneOffset.UTC.

Learn about Servlet context listeners setting objects as key-value pairs in the “attributes” store on the Servlet context. You might store your desired ZoneId object there. Or perhaps Spring has some way to help with this. (I do not use Spring.)

See my Answer to this similar Question for more info.

Upvotes: 1

Related Questions