Spring boot 時間格式轉換(格林威治時間格式轉為2019-01-01 10:10:10格式)


如果不進行時間格式轉換,直接從數據庫中查詢到的數據是格林威治時間格式的(Tue Jan 01 00:00:00 CST 2019)。轉后為(2019-01-01 10:10:10 )。

一、自定義一個轉換類用於時間格式的轉換。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
    
    //日期轉字符串
    public static String dateToStr(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");    //如果需要顯示時分秒:"yyyy/MM/dd HH:mm:ss"
        return sdf.format(date);
    }
    
    //字符串轉日期
    public static Date strToDate(String str) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

 

二、在實體類中繼承這個類,並調用方法

方法1、設置一個虛擬變量

 

import java.io.Serializable;
import java.util.Date;


import cn.mg39.ssm.util.DateUtil;

public class LeaveApply extends DateUtil implements Serializable {
    private int id;
    private LeaveType leaveType;        //請假類型
    private String leaveReason;            //請假原因
    private Date beginTime;        //開始時間
    private Date endTime;        //結束時間
    private EmpEmployee applyEmp;        //申請人
    private int status;                    //請假狀態
    private EmpEmployee checkEmp;        //審核者
    /**
     * 設置虛擬變量(中間變量,通過它將數據庫中查詢到的值和頁面傳進去的值進行轉換)
     */
    private String strBeginTime;        //處理時間的字符串
    private String strEndTime;


    public String getStrBeginTime() {
        return strBeginTime;
    }

    public void setStrBeginTime(String strBeginTime) {
        if (strBeginTime != null) {
            this.beginTime = strToDate(strBeginTime);
        }
        this.strBeginTime = strBeginTime;
    }

    public String getStrEndTime() {
        return strEndTime;
    }

    public void setStrEndTime(String strEndTime) {
        if (strEndTime != null) {
            this.endTime = strToDate(strEndTime);
        }
        this.strEndTime = strEndTime;
    }


    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        if (beginTime != null) {
            this.strBeginTime = dateToStr(beginTime);
        }
        this.beginTime = beginTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        if (endTime != null) {
            this.strEndTime = dateToStr(endTime);
        }
        this.endTime = endTime;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public LeaveType getLeaveType() {
        return leaveType;
    }

    public void setLeaveType(LeaveType leaveType) {
        this.leaveType = leaveType;
    }

    public String getLeaveReason() {
        return leaveReason;
    }

    public void setLeaveReason(String leaveReason) {
        this.leaveReason = leaveReason;
    }
    
    /*
    public String getBeginTime() {
        return dateToStr(beginTime);        //從數據庫中查詢到的數據轉為字符串格式
    }
    
    public void setBeginTime(String beginTime) {
        this.beginTime = strToDate(beginTime);        //將字符串轉為date格式傳到數據庫
    }
    public String getEndTime() {
        return dateToStr(endTime);
    }
    public void setEndTime(String endTime) {
        this.endTime = strToDate(endTime);
    }*/


    public EmpEmployee getApplyEmp() {
        return applyEmp;
    }

    public void setApplyEmp(EmpEmployee applyEmp) {
        this.applyEmp = applyEmp;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public EmpEmployee getCheckEmp() {
        return checkEmp;
    }

    public void setCheckEmp(EmpEmployee checkEmp) {
        this.checkEmp = checkEmp;
    }

    public LeaveApply() {
        super();
    }

    public LeaveApply(int id, LeaveType leaveType, String leaveReason, Date beginTime, Date endTime,
                      EmpEmployee applyEmp, int status, EmpEmployee checkEmp) {
        super();
        this.id = id;
        this.leaveType = leaveType;
        this.leaveReason = leaveReason;
        this.beginTime = beginTime;
        this.endTime = endTime;
        this.applyEmp = applyEmp;
        this.status = status;
        this.checkEmp = checkEmp;
    }

    public LeaveApply(int id) {
        super();
        this.id = id;
    }

    @Override
    public String toString() {
        return "LeaveApply [id=" + id + ", leaveType=" + leaveType + ", leaveReason=" + leaveReason + ", beginTime="
                + beginTime + ", endTime=" + endTime + ", applyEmp=" + applyEmp + ", status=" + status + ", checkEmp="
                + checkEmp + ", strBeginTime=" + strBeginTime + ", strEndTime=" + strEndTime + "]";
    }


}

 

 

方法2、直接改返回值等

import java.io.Serializable;
import java.util.Date;
import java.util.List;


import cn.mg39.ssm.util.DateUtil;

public class LeaveApply extends DateUtil implements Serializable {
    private int id;
    private LeaveType leaveType;        //請假類型
    private String leaveReason;            //請假原因
    private Date beginTime; //開始時間 private Date endTime; //結束時間
    private EmpEmployee applyEmp;        //申請人
    private int status;                    //請假狀態
    private EmpEmployee checkEmp;        //審核者
    
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public LeaveType getLeaveType() {
        return leaveType;
    }
    public void setLeaveType(LeaveType leaveType) {
        this.leaveType = leaveType;
    }
    public String getLeaveReason() {
        return leaveReason;
    }
    public void setLeaveReason(String leaveReason) {
        this.leaveReason = leaveReason;
    }
    
    
    
    public String getBeginTime() {
        return dateToStr(beginTime);        //從數據庫中查詢到的數據轉為字符串格式
    }
    
    public void setBeginTime(String beginTime) {
        Date dd =strToDate(beginTime);        //將字符串轉為date格式傳到數據庫
        this.beginTime = dd;        
    }
    public String getEndTime() {
        return dateToStr(endTime);
    }
    public void setEndTime(String endTime) {
        Date dd =strToDate(endTime);
        this.endTime = dd;
    }
    
    
    public EmpEmployee getApplyEmp() {
        return applyEmp;
    }
    public void setApplyEmp(EmpEmployee applyEmp) {
        this.applyEmp = applyEmp;
    }
    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public EmpEmployee getCheckEmp() {
        return checkEmp;
    }
    public void setCheckEmp(EmpEmployee checkEmp) {
        this.checkEmp = checkEmp;
    }
    
    
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM