数据库存储时间的几种方式

在平时的开发中,会在数据库中记录该条记录相关的操作时间,或业务上要用到的时间格式

正常使用中有以下三种类型:

1. 使用Timestamp,则会在数据库里存储:2017-12-21 07:20:01。
在不同时区,显示的都是2017-12-21 07:20:01,但其实他们并不是同一时间了。

2. 存储事件发生的时间毫秒值,在不同时区解析出来的时间表示不一样,但表达都是同一时间,能解决时区问题。

3. 直接是Date类型是数据格式,存储年月日。

在数据库里头的展示格式如图所示:

Java代码里头获取的时间如图所示:

1.TIMESTAMP格式 

afaUser.setLastestLogin(new Date()); 

2.时间毫秒级 

loginlog.setLastModifyTime(new Date().getTime()); 

3.Date类型 

afaUser.setEndDate(new Date()); 
   SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
   try {
        afaUser.setEndDate(sf.parse(sf.format(new Date())));
        afaUser.setStartDate(sf.parse(sf.format(new Date())));
    } catch (ParseException e) {            
    e.printStackTrace();
    } 

 格式化注意由于数据库是Date类型,所以无论怎么格式化,还是2017/12/21这种格式。   

在往前端传值的时候,若是TIMESTAMP类型,前端获取的是long类型的时间戳,这时候就要进行格式转换。所以这种格式比较麻烦,尽量少用。

一、在实体类中进行转换

    @Column(name = "LASTEST_LOGIN")
    private Date lastestLogin;         //返回前端会是毫秒时间戳。
    
    @Transient
    private String lastLoginDate;      //新建一个字段,将lastestLogin格式转化所需要的时间格式。
    
    public String getLastLoginDate() {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if(this.lastestLogin!=null){
                lastLoginDate=sf.format(lastestLogin);
            }
        } catch (Exception e) {
        }
        return lastLoginDate;
    }

    public void setLastLoginDate(String lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

二、在前端web进行转换

<script language="javascript">   
    //扩展Date的format方法 
    Date.prototype.format = function (format) {
        var o = {
            "M+": this.getMonth() + 1,
            "d+": this.getDate(),
            "h+": this.getHours(),
            "m+": this.getMinutes(),
            "s+": this.getSeconds(),
            "q+": Math.floor((this.getMonth() + 3) / 3),
            "S": this.getMilliseconds()
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }
    /**  
    *转换日期对象为日期字符串  
    * @param date 日期对象  
    * @param isFull 是否为完整的日期数据,  
    *               为true时, 格式如"2000-03-05 01:05:04"  
    *               为false时, 格式如 "2000-03-05"  
    * @return 符合要求的日期字符串  
    */  
    function getSmpFormatDate(date, isFull) {
        var pattern = "";
        if (isFull == true || isFull == undefined) {
            pattern = "yyyy-MM-dd hh:mm:ss";
        } else {
            pattern = "yyyy-MM-dd";
        }
        return getFormatDate(date, pattern);
    }
    /**  
    *转换当前日期对象为日期字符串  
    * @param date 日期对象  
    * @param isFull 是否为完整的日期数据,  
    *               为true时, 格式如"2000-03-05 01:05:04"  
    *               为false时, 格式如 "2000-03-05"  
    * @return 符合要求的日期字符串  
    */  

    function getSmpFormatNowDate(isFull) {
        return getSmpFormatDate(new Date(), isFull);
    }
    /**  
    *转换long值为日期字符串  
    * @param l long值  
    * @param isFull 是否为完整的日期数据,  
    *               为true时, 格式如"2000-03-05 01:05:04"  
    *               为false时, 格式如 "2000-03-05"  
    * @return 符合要求的日期字符串  
    */  

    function getSmpFormatDateByLong(l, isFull) {
        return getSmpFormatDate(new Date(l), isFull);
    }
    /**  
    *转换long值为日期字符串  
    * @param l long值  
    * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss  
    * @return 符合要求的日期字符串  
    */  

    function getFormatDateByLong(l, pattern) {
        return getFormatDate(new Date(l), pattern);
    }
    /**  
    *转换日期对象为日期字符串  
    * @param l long值  
    * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss  
    * @return 符合要求的日期字符串  
    */  
    function getFormatDate(date, pattern) {
        if (date == undefined) {
            date = new Date();
        }
        if (pattern == undefined) {
            pattern = "yyyy-MM-dd hh:mm:ss";
        }
        return date.format(pattern);
    }
 
    //alert(getSmpFormatDate(new Date(1279849429000), true));
    //alert(getSmpFormatDate(new Date(1279849429000),false));    
    //alert(getSmpFormatDateByLong(1279829423000, true));
    alert(getSmpFormatDateByLong(1279829423000,false));    
    //alert(getFormatDateByLong(1279829423000, "yyyy-MM"));
    //alert(getFormatDate(new Date(1279829423000), "yy-MM"));
    //alert(getFormatDateByLong(1279849429000, "yyyy-MM hh:mm"));     
</script> 

 所以在实际使用过程中可以使用Date或者事件发生的时间毫秒值,少用TIMESTAMP类型进行数据的存储。

import java.text.*;
import java.util.Date;

/**
  SimpleDateFormat函数语法:
  
  G 年代标志符
  y 年
  M 月
  d 日
  h 时 在上午或下午 (1~12)
  H 时 在一天中 (0~23)
  m 分
  s 秒
  S 毫秒
  E 星期
  D 一年中的第几天
  F 一月中第几个星期几
  w 一年中第几个星期
  W 一月中第几个星期
  a 上午 / 下午 标记符 
  k 时 在一天中 (1~24)
  K 时 在上午或下午 (0~11)
  z 时区
 */
public class FormatDateTime {

    public static void main(String[] args) {
        SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm"); 
        SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
        SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
        SimpleDateFormat myFmt4=new SimpleDateFormat(
                "一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
        Date now=new Date();
        System.out.println(myFmt.format(now));
        System.out.println(myFmt1.format(now));
        System.out.println(myFmt2.format(now));
        System.out.println(myFmt3.format(now));
        System.out.println(myFmt4.format(now));
        System.out.println(now.toGMTString());
        System.out.println(now.toLocaleString());
        System.out.println(now.toString());
    }    
    
}

效果如下:

效果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四 
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
原文地址:https://www.cnblogs.com/shawWey/p/8079244.html