JsonFormat 后日期少了8個小時什么鬼胆萧?
前言
今天測試的時候發(fā)現(xiàn)時間對不上,比數(shù)據(jù)庫里的時間少了8個小時?測試小姐姐一頓狂轟亂炸跌穗,一點都不溫柔订晌。
什么鬼?哪里出了問題蚌吸?
數(shù)據(jù)庫顯示的是下面??
在這里插入圖片描述
畫面顯示如下
在這里插入圖片描述
我的數(shù)據(jù)里明明顯示的是對的時間锈拨,怎么到畫面顯示你就少了8個小時?
快羹唠,還我8個小時奕枢。
扯遠(yuǎn)了,趕緊擼代碼佩微,找問題缝彬。
數(shù)據(jù)庫里顯示的是
2020-03-17 11:40:27
然而到了畫面前端顯示的是
2020-03-17 03:40:27
分析
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
應(yīng)該就是上面的代碼出了問題,沒關(guān)系哺眯,出問題慢慢解決谷浅。
看看源碼
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonFormat {
String DEFAULT_LOCALE = "##default";
String DEFAULT_TIMEZONE = "##default";
String pattern() default "";
JsonFormat.Shape shape() default JsonFormat.Shape.ANY;
String locale() default "##default";
String timezone() default "##default";
JsonFormat.Feature[] with() default {};
JsonFormat.Feature[] without() default {};
}
JsonFormat 是一個注解,上面的createTime族购,我們配置的pattern時間的格式壳贪,其他的都是默認(rèn)的。
少了8個小時寝杖?是不是會是時區(qū)的問題违施,接著往下面看,眼前一亮呀。
//java.util.TimeZone to use for serialization (if needed)
public String timezone() default DEFAULT_TIMEZONE;
不設(shè)置時區(qū)的話瑟幕,會有個默認(rèn)的時區(qū)磕蒲。
網(wǎng)上找找代碼看看時區(qū)的數(shù)據(jù)
public static void main(String[] args) {
System.out.println(TimeZone.getDefault().getID());
String[] ids = TimeZone.getAvailableIDs();
for (String id : ids) {
System.out.println(displayTimeZone(TimeZone.getTimeZone(id)));
}
System.out.println("\nTotal TimeZone ID " + ids.length);
}
private static String displayTimeZone(TimeZone tz) {
long hours = TimeUnit.MILLISECONDS.toHours(tz.getRawOffset());
long minutes = TimeUnit.MILLISECONDS.toMinutes(tz.getRawOffset())
- TimeUnit.HOURS.toMinutes(hours);
// avoid -4:-30 issue
minutes = Math.abs(minutes);
String result = "";
if (hours > 0) {
result = String.format("(GMT+%d:%02d) %s", hours, minutes, tz.getID());
} else {
result = String.format("(GMT%d:%02d) %s", hours, minutes, tz.getID());
}
return result;
}
輸出結(jié)果
Asia/Shanghai
(GMT-12:00) Etc/GMT+12
(GMT-11:00) Etc/GMT+11
(GMT-11:00) Pacific/Midway
(GMT-11:00) Pacific/Niue
(GMT-11:00) Pacific/Pago_Pago
(GMT-11:00) Pacific/Samoa
(GMT-11:00) US/Samoa
(GMT-10:00) America/Adak
(GMT-10:00) America/Atka
(GMT-10:00) Etc/GMT+10
(GMT-10:00) HST
(GMT-10:00) Pacific/Honolulu
(GMT-10:00) Pacific/Johnston
(GMT-10:00) Pacific/Rarotonga
(GMT-10:00) Pacific/Tahiti
(GMT-10:00) SystemV/HST10
(GMT-10:00) US/Aleutian
(GMT-10:00) US/Hawaii
(GMT-9:30) Pacific/Marquesas
省略……
Total TimeZone ID 620
我們在看看
public TimeZone getTimeZone() {
TimeZone tz = this._timezone;
if (tz == null) {
if (this._timezoneStr == null) {
return null;
}
tz = TimeZone.getTimeZone(this._timezoneStr);
this._timezone = tz;
}
return tz;
}
/**
* Gets the <code>TimeZone</code> for the given ID.
*
* @param ID the ID for a <code>TimeZone</code>, either an abbreviation
* such as "PST", a full name such as "America/Los_Angeles", or a custom
* ID such as "GMT-8:00". Note that the support of abbreviations is
* for JDK 1.1.x compatibility only and full names should be used.
*
* @return the specified <code>TimeZone</code>, or the GMT zone if the given ID
* cannot be understood.
*/
public static synchronized TimeZone getTimeZone(String ID) {
return getTimeZone(ID, true);
}
private static TimeZone getTimeZone(String ID, boolean fallback) {
TimeZone tz = ZoneInfo.getTimeZone(ID);
if (tz == null) {
tz = parseCustomTimeZone(ID);
if (tz == null && fallback) {
tz = new ZoneInfo(GMT_ID, 0);
}
}
return tz;
}
timezone
1.1 什么是時區(qū)?
timezone只盹,即由于世界各國家與地區(qū)經(jīng)度不同辣往,地方時也有所不同,按照經(jīng)度將全球劃分為24個時區(qū)殖卑。
由于世界各國家與地區(qū)經(jīng)度不同站削,地方時也有所不同,因此會劃分為不同的時區(qū)孵稽。
時區(qū)有相應(yīng)的英文字母縮寫许起,例如GMT,UTC,CST等,常見的時區(qū)菩鲜。