Home >Java >javaTutorial >How to solve the problem of springboot configuring date field to return timestamp

How to solve the problem of springboot configuring date field to return timestamp

王林
王林forward
2023-05-20 11:16:452403browse

I encountered a problem. After springboot was upgraded to 2.0, the date found from the database was received using Date, and finally returned directly to the front end. It can be displayed normally as yyyy-MM-dd HH:mm:ss in Google Chrome. Format. However, the date displayed in the IE browser is "garbled" because the springboot1.x version returns a timestamp in the Date field by default, and Google and IE will automatically convert the timestamp into yyyy-MM-dd HH:mm: ss; after springboot2.0, spring will automatically convert the Date field into a UTC string (without configuration), so the date needs to be converted into a timestamp or yyyy-MM-dd HH:mm:ss. Add the following configuration to spring. I chose the return timestamp configuration and successfully solved the problem.

Configuration returns timestamp

spring
jackson:
serialization:
write-dates-as-timestamps:true

How to solve the problem of springboot configuring date field to return timestamp

Global configuration returns string

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

With the second configuration, there is no need to configure Add the @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT 8") annotation to the date attribute of each entity class

# 指定执行环境
spring:
  profiles:
    # active: @env@
    active: dev
  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: true

The above is the detailed content of How to solve the problem of springboot configuring date field to return timestamp. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete