vue+elementui+springboot+jersey+mysql time zone summary

Question: The date obtained from the front-end el-date-picker control, the date passed to the java side is reduced by one day, and the date is reduced by one day when stored in the database.

The main reason: It is mainly caused by the inconsistent time zones of the front-end and back-end databases.

Solution: Unify the time zone (or use all strings, only the time zone is introduced here).

CommonConst class uniformly sets the time zone string

public static final String JAVA_TIME_ZONE = "CST";

1. The front-end formats the time object obtained from the control into a string and transmits it to the background. Otherwise, although the date seems to be correct, the transmission will be one day less. String comparison is appropriate.

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

formatDate function reference:

2. Set the time zone and format when receiving the backend jersey

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

You can also use java to convert

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 ();
}

3. The time zone of the database is preferably unified, which can be achieved by modifying the serverTimezone parameter of the connection string:

jdbc:mysql://*******:3506/*****?useUnicode=true&characterEncoding=utf-8&serverTimezone=CST&useSSL=true

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325303780&siteId=291194637