全局时间格式设置

If we want to configure a default format for all dates in our application, a more flexible way is to configure it in application.properties:

1
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

And if we want to use a specific time zone in our JSON dates, there's also a property for that:

1
spring.jackson.time-zone=Europe/Zagreb

Although setting the default format like this is quite handy and straightforward, there's a drawback to this approach. Unfortunately, it doesn't work with the Java 8 date types, like LocalDate and LocalDateTime – we can use it only to format fields of the type java.util.Date or the java.util.Calendar. There is hope, though, as we'll soon see.

参考:https://www.baeldung.com/spring-boot-formatting-json-dates


平常项目使用:时间类型设置成Date,返回给前端的时间格式会自动转换

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