vue+elementui+springboot+jersey+mysql時區總結

問題:從前端el-date-picker控件獲得的日期,傳到java端日期減小一天,存到數據庫中又減小一天。前端

主要緣由:主要是先後端數據庫時區不統一致使。java

解決:將時區統一(或所有使用字符串,這裏只介紹時區)。mysql

自定義類CommonConst統一設置時區字符串sql

public static final String JAVA_TIME_ZONE = "CST";

一、前端將從控件獲得的時間對象格式化成字符串傳到後臺,不然雖然看上去日期是正確的可是傳送的時候會少一天。用字符串比較穩當。數據庫

var begindate = formatDate(this.datediff[0], 'yyyy-MM-dd') || ''後端

formatDate函數參考:函數

二、後端jersey接收時設置時區和格式this

@JsonFormat(timezone = CommonConst.JAVA_TIME_ZONE, pattern = "yyyy-MM-dd")
@FormParam("enddate")
private Date enddate;

也能夠使用java轉換code

DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
dataFormat.setTimeZone(TimeZone.getTimeZone(CommonConst.JAVA_TIME_ZONE));
Date beginDate = null;
Date endDate = null;
try {
    beginDate = dataFormat.parse(beginDateStr);
    endDate = dataFormat.parse(endDateStr);
} catch (ParseException e) {
    e.printStackTrace();
}

三、數據庫的時區最好也統一,能夠經過修改鏈接字符串serverTimezone參數實現:orm

jdbc:mysql://*******:3506/*****?useUnicode=true&characterEncoding=utf-8&serverTimezone=CST&useSSL=true
相關文章
相關標籤/搜索