Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...