Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a Spring Boot JVM into UTC time zone?

Tags:

I saw Force Java timezone as GMT/UTC

I tried

  • mvn spring-boot:run -Dexec.args="-Duser.timezone=GMT"
  • mvn spring-boot:run -Dexec.args="-Duser.timezone=UTC"
  • user.timezone=UTC in config/application.properties
  • user.timezone=GMT
  • In the 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> 
  • mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"

But it prints out

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

like image 406
Chloe Avatar asked Jan 22 '19 21:01

Chloe


People also ask

How do I set UTC timezone in spring boot?

In order to select our timezone, we have to add the connectionTimeZone property to specify the timezone: spring: datasource: url: jdbc:mysql://localhost:3306/test?connectionTimeZone=UTC username: root password: Also, we can, of course, configure the datasource with Java configuration instead.

How do I change the timezone in JVM?

In windows, we can set the time zone as discussed using the Control Panel -> Date and Time -> Change Time Zone -> Select your preferred time zone option. After setting the environment variable, we can verify it in our Java program. TimeZone timezone = TimeZone. getDefault(); System.

What is JVM default timezone?

By default, the JVM reads time zone information from the operating system. This information gets passed to the TimeZone class, which stores the time zone and calculates the daylight saving time. We can call the method getDefault, which will return the time zone where the program is running.

What is UTC timezone in Java?

Java For Testers UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.


1 Answers

I think you can set your application's timezone on your application level. I think this link will help you. https://www.onlinetutorialspoint.com/spring-boot/how-to-set-spring-boot-settimezone.html

So What you need to do is adding "@PostConstruct" annotation to the main class where "@SpringBootApplication" annotation is located, and add timezone setting method there. Here is an example.

@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"));     }  } 

Hope this can help you!

like image 58
YONGSOO KIM Avatar answered Sep 19 '22 05:09

YONGSOO KIM