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

王林
Release: 2023-05-20 11:16:45
forward
2444 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template