知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
經過檢查:操作系統時區 CST,資料庫是 SYSTEM,Spring boot 獲取時間相差8個小時。
分析:認為是 @JsonFormat 格式化造成的。
解決方案:在 @JsonFormat 中增加時區設置。
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") public Date ctime;
期間嘗試多種方式無效:
# 下面例子無效
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.mvc.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# 下面方法無效
spring.datasource.url=jdbc:mysql://119.29.241.95:3306/5kwords?useSSL=false&serverTimezone=UTC
# 下面配置仍然無效
spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true
spring.jpa.properties.jadira.usertype.javaZone=Asia/Shanghai
spring.jpa.properties.jadira.usertype.databaseZone=Asia/Shanghai
根源在 Json 轉化。
完成的例子
package common.domain; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonFormat; @Entity @Table(name = "article", catalog = "cms") public class Article implements Serializable { private static final long serialVersionUID = 7603772682950271321L; @Id public int id; public String title; @Column(name = "short") public String shortTitle; public String description; public String author; public String star; public String tags; public boolean status; public String content; public int typeId; public int siteId; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") public Date ctime; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "America/Phoenix") public Date mtime; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public Date getCtime() { return ctime; } public void setCtime(Date ctime) { this.ctime = ctime; } public String getShortTitle() { return shortTitle; } public void setShortTitle(String shortTitle) { this.shortTitle = shortTitle; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getStar() { return star; } public void setStar(String star) { this.star = star; } public String getTags() { return tags; } public void setTags(String tags) { this.tags = tags; } public boolean isStatus() { return status; } public void setStatus(boolean status) { this.status = status; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getTypeId() { return typeId; } public void setTypeId(int typeId) { this.typeId = typeId; } public int getSiteId() { return siteId; } public void setSiteId(int siteId) { this.siteId = siteId; } public Date getMtime() { return mtime; } public void setMtime(Date mtime) { this.mtime = mtime; } @Override public String toString() { return "Article [id=" + id + ", title=" + title + ", shortTitle=" + shortTitle + ", description=" + description + ", author=" + author + ", star=" + star + ", tags=" + tags + ", status=" + status + ", content=" + content + ", typeId=" + typeId + ", siteId=" + siteId + ", ctime=" + ctime + ", mtime=" + mtime + "]"; } }