Spring boot+mongodb time inconsistency problem

      In the application, set the creation time for an entity object, use java.util.Date type, and directly generate new Date(). After saving to Mongodb, it is found that the value of this field is 8 hours less, that is, 8 hours later than the actual saving time . The reason is because Mongodb saves the time in standard Greenwich Mean Time (GMT) and does not set the local time zone, and Beijing time belongs to the East Eighth District, which is eight hours earlier than it. In fact, the two time values ​​represent the same time, but the data returned during the query is not displayed normally. In order to display the time seen by the client as Beijing time, you can add a json parsing time zone configuration to the spring boot configuration file. That's it (jackson is used here as the json parsing framework, other frameworks are similar): spring.jackson.timeZone=GMT+8.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326266628&siteId=291194637