21
votes

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

5
This works for Spring Boot v1: mvn spring-boot:run -Drun.jvmArguments="-Duser.timezone=UTC"Chloe

5 Answers

23
votes

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
45
votes

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!

19
votes

You can configure the timezone with a class annotated with the @Configuration annotation. You can put it anywhere in your project. I typically house all classes that fit under this category in a package called config. Make sure you add the @PostConstruct annotation to the method that is actually setting the timezone.

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());
    }
}

See the original article

0
votes

More options in case your application is running under linux:

Set TZ environment variable

See "In POSIX systems, a user can specify the time zone by means of the TZ environment variable" (https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html).

This option is especially useful in any cloud environment.

Symlink /etc/locatime

I.e. in my local system /etc/locatime symlinks to /usr/share/zoneinfo/Europe/Berlin:

➜ ls -l /etc/localtime  
lrwxrwxrwx 1 root root 33 22. Jan 23:01 /etc/localtime -> /usr/share/zoneinfo/Europe/Berlin

You can easily change the symbolic link with ln -s /usr/share/zoneinfo/GMT /etc/localtime, the possible values can be found within /usr/share/zoneinfo/.

This option can also be used in many cloud environments by mounting a hostvolume, see kubernetes timezone in POD with command and argument.

-1
votes

<properties> <spring-boot.run.jvmArguments>-Duser.timezone=UTC</spring-boot.run.jvmArguments> </properties> That did not work for me.

But if you use jvmArguments instead that worked for me. Reference: https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/html/

simply <configuration> <jvmArguments>-Duser.timezone=UTC</jvmArguments> </configuration>