Search code examples
javaspringspring-boottimezone

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


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


Solution

  • Use spring-boot.run.jvmArguments property if you want to pass JVM options from Maven Spring Boot Plugin to forked Spring Boot application:

    <properties>
      <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments>
    </properties>
    

    This is be equivalent to command line syntax:

    mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Duser.timezone=UTC"
    

    or when running a fully packaged Spring Boot application:

    java -Duser.timezone=UTC -jar app.jar