前言
今天出現(xiàn)了一個(gè)小BUG,時(shí)間類型數(shù)據(jù)格式化時(shí)出現(xiàn)誤差(八小時(shí))塌衰。
數(shù)據(jù)庫是MySQL,字段類型為datetime
。
Java成員屬性類型為Timestamp
枕赵。
使用了@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
注解,雖然能解析但是卻有八小時(shí)的誤差驯遇,最終原因是未設(shè)置時(shí)區(qū)引起的焙畔。
項(xiàng)目中導(dǎo)入所需Jar包
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0.pr3</version>
</dependency>
@JsonFormat注解
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.date;
import java.sql.Timestamp;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date create_time;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Timestamp update_time;
//pattern設(shè)置格式化的格式
//timezone設(shè)置時(shí)區(qū)(不設(shè)置有可能出現(xiàn)八小時(shí)的誤差)