週五. 8 月 7th, 2020

IT Skills 波林

Polin WEI – 資訊工作者的技術手札

java 對於 Element UI 的 DateTimePicker : el-date-picker 在時區上的處理

1 min read
安妮公主

java 對於 Element UI 的 DateTimePicker : el-date-picker 在時區上的處理

 

formatmeaningnoteexample
yyyyyear2017
Mmonthno leading 01
MMmonth1
MMMmonthJan
MMMMmonthJanuary
Wweekonly for week picker's format; no leading 01
WWweekonly for week picker's format1
ddayno leading 02
ddday2
Hhour24-hour clock; no leading 03
HHhour24-hour clock3
hhour12-hour clock; must be used with A or a; no leading 03
hhhour12-hour clock; must be used with A or a3
mminuteno leading 04
mmminute4
ssecondno leading 05
sssecond5
AAM/PMonly for format, uppercasedAM
aam/pmonly for format, lowercasedam
timestampJS timestamponly for value-format; binding value will be a number1483326245000
[MM]No escape charactersTo escape characters, wrap them in square brackets (e.g. [A] [MM])MM

 

它提供多項的屬性 ( Attributes )來滿足 UI 上的需求,若沒有設定它的 Date Format ,它預設的 Date Formats 格式為 yyyy-mm-dd HH:mm:ss

<div class="row">
<div class="col-sm-6">
<el-form-item prop="activeDate" label="activeDate" >
 <el-date-picker v-model="formData.activeDate"  type="datetime" placeholder="Select  date and time"  default-time="08:00:00" :picker-options="afterToday" > </el-date-picker>
</el-form-item>
</div>
<div class="col-sm-6">
<el-form-item prop="inactiveDate" label="inactiveDate" >
 <el-date-picker v-model="formData.inactiveDate"  type="datetime" placeholder="Select date and time" default-time="17:30:00" :picker-options="beforeToday"> </el-date-picker>
</el-form-item>
</div>                      
</div>

 

當後送到主機時,資料型態為 JSON 類似: "activeDate":"2020-03-24T03:25:52.000Z",是有帶時區的時間。那麼在後端主機的 Java 程式要如何承接轉譯呢? 可以透過下列的程式碼即可。

/**
 * 取得 Element UI 的 el-date-picker 回傳值, 轉換成 Timestamp
 * @param elDatePicker
 * @return
 * @throws ParseException
 */
public Timestamp getTimestamp(Object elDatePicker) throws ParseException {
 if (Objects.isNull(elDatePicker)) {
  return null;
 } else {
  String strDate = elDatePicker.toString();
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
  df.setTimeZone(TimeZone.getTimeZone("GMT"));
  Date date = df.parse(strDate);
  return new Timestamp(date.getTime());
 }
}

 

 

Copyright © All rights reserved. | Newsphere by AF themes.