zoukankan      html  css  js  c++  java
  • 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;
        }
        
        
    }

  • 相关阅读:
    open_basedir restriction in effect的错误及其解决办法
    SNMP-网络管理协议
    安装cacti监控系统
    并发时-修改Linux系统下的最大文件描述符限制
    js new date()说明
    阿里云ECS环境部署 centos 6.5
    sysbench
    http_load
    LeetCode: Spiral Matrix
    LeetCode:Length of Last Word
  • 原文地址:https://www.cnblogs.com/zhangzimuzjq/p/12131856.html
Copyright © 2011-2022 走看看