SoFunction
Updated on 2025-04-08

How to troubleshoot the problem of 8 hours difference between the data time and actual data returned by SpringBoot interface

The problem of 8 hours difference between the data time returned by the SpringBoot interface

Scene description

A SpringBoot application is deployed in the container and requests an interface. The time returned by the interface is 8 hours different from the actual situation.

Possible Causes

1. The time zone of the container is different from the actual time zone by 8 hours.

2. The jvm time zone is 8 hours apart from the actual time zone

3. The time difference after storage in the database is 8 hours

4. The time of the backend acquisition is the same, but the difference is 8 hours after returning to the frontend.

Troubleshooting steps

1. Enter the container to view the time

$ date

2. Write a java application to view jvm time

import ;

public class Demo {
   public static void main(String[] args) {
       Date date = new Date();
       (date);
   }
}

3. Check the database time

Open the database and see if the database time stored in the database is consistent with the actual

4. Is the time when the data obtained by the backend is consistent with the frontend?

Solution

The container time zone is 8 hours apart from the actual time zone:

If the time obtained in the first step of troubleshooting is 8 hours different from the actual time, it is recommended to add the following content to the Dockerfile of the container:

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone

The jvm time zone is 8 hours different from the actual time zone:

If the time obtained in the second step of the troubleshooting process is 8 hours different from the actual time, you can add the following code to the SpringBoot oriented section or main class

@PostConstruct
void started() {
    (("GMT+8"));
}

The time to be stored in the database is 8 hours different from the actual time zone:

If the time obtained in the third step of the troubleshooting process is 8 hours apart from the actual time, you can modify the database connection information in the SpringBoot application configuration file and add it at the end of the urlserverTimeZone=GMT%2b8

Spring's json constructor causes time zone inconsistency:

The fundamental reason for the above three situations is that the initial time zone of the environment is not configured. The best solution is to configure their respective time zones before the application is deployed.

The fourth step of troubleshooting time zone inconsistency occurs, which is because the time zone of spring's json constructor is inconsistent with the actual situation.

You can modify the springboot configuration file and add the following content:

spring:
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.