一:Spring boot @ResponseBody轉換JSON 時 Date 時間相差8小時
一描述:當使用jackson時,返回的json和數據庫會相差8個小時,(親測);
數據庫和postman相差8個小時,debug後發現是jackson得問題。
二解決辦法:
#application.yml文件配置
spring:
jackson:
time-zone: GMT+8
或者這樣也可以解決,在你的時間上設置時間格式化。
public class Vo {
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createTime;
}
參考來自:https://blog.csdn.net/CLG_CSDN/article/details/98180387
二:mysql從數據庫查詢的時間與實際時間相差8小時
一解決辦法:
設置spring配置文件:
1、spring.datasource.url=jdbc:mysql://10.35.105.25:3306/database?characterEncoding=utf-8&serverTimezone=GMT%2B8
數據庫配置後面加上&serverTimezone=GMT%2B8
2、spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#application.yml文件配置
spring:
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
第一步爲設置數據庫時間爲東八區(北京)時間,保證debug時候從數據庫查出時間一致。第二步爲返回給前端的時間格式和時區設定,保證前端頁面顯示時間和數據庫一致。