JsonFormat 日期少了8個小時?還我

JsonFormat 後日期少了8個小時什麼鬼?

前言

今天測試的時候發現時間對不上,比數據庫裏的時間少了8個小時?測試小姐姐一頓狂轟亂炸,一點都不溫柔。

什麼鬼?哪裏出了問題?
數據庫顯示的是下面👇
在這裏插入圖片描述
畫面顯示如下
在這裏插入圖片描述

我的數據裏明明顯示的是對的時間,怎麼到畫面顯示你就少了8個小時?

快,還我8個小時。

扯遠了,趕緊擼代碼,找問題。

數據庫裏顯示的是
2020-03-17 11:40:27

然而到了畫面前端顯示的是
2020-03-17 03:40:27

分析

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

應該就是上面的代碼出了問題,沒關係,出問題慢慢解決。

看看源碼


@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時間的格式,其他的都是默認的。

少了8個小時?是不是會是時區的問題,接着往下面看,眼前一亮呀。

    //java.util.TimeZone to use for serialization (if needed)
    public String timezone() default DEFAULT_TIMEZONE;

不設置時區的話,會有個默認的時區。

網上找找代碼看看時區的數據

 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;

    }

輸出結果

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 什麼是時區?
timezone,即由於世界各國家與地區經度不同,地方時也有所不同,按照經度將全球劃分爲24個時區。

由於世界各國家與地區經度不同,地方時也有所不同,因此會劃分爲不同的時區。

時區有相應的英文字母縮寫,例如GMT,UTC,CST等,常見的時區。

很多細節要注意呀。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章