While run any application in JVM, JVM will take system default time zone. For example production server is running under PST timezone and spring boot application will start then application will take PST timezone by default.

We want to start application with another timezone then need to set timezone like :   TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

Example:

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {

    @PostConstruct
    public void init(){
        TimeZone.setDefault(TimeZone.getTimeZone("UTC"));   // It will set UTC timezone
        System.out.println("Spring boot application running in UTC timezone :"+new Date());   // It will print UTC timezone
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

Output

Spring boot application running in UTC timezone Sat Nov 11 13:28:43 UTC 2017
2017-11-11 18:58:43.708  INFO 169692 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]49e5f737: startup date [Sat Nov 11 13:28:40 UTC 2017]; root of context hierarchy
2017-11-11 18:58:43.802  INFO 169692 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/demo2]}" onto public java.lang.String com.javadeveloperzone.controller.CompressDemoController.demo()
2017-11-11 18:58:43.810  INFO 169692 --- [

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *