首页 > 编程语言 > 详细

Spring boot 时间格式转换(格林威治时间格式转为2019-01-01 10:10:10格式)

时间:2020-01-02 13:16:21      阅读:402      评论:0      收藏:0      [点我收藏+]

如果不进行时间格式转换,直接从数据库中查询到的数据是格林威治时间格式的(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;
    }
}

 

二、在实体类中继承这个类,并调用方法

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

Spring boot 时间格式转换(格林威治时间格式转为2019-01-01 10:10:10格式)

原文:https://www.cnblogs.com/zhangzimuzjq/p/12131856.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!