數(shù)據(jù)庫創(chuàng)建時(shí)間的幾種方式

正常使用中有以下三種類型:


1. 使用Timestamp,則會在數(shù)據(jù)庫里存儲:2017-12-21 07:20:01。

在不同時(shí)區(qū),顯示的都是2017-12-21 07:20:01,但其實(shí)他們并不是同一時(shí)間了。


2. 存儲事件發(fā)生的時(shí)間毫秒值,在不同時(shí)區(qū)解析出來的時(shí)間表示不一樣,但表達(dá)都是同一時(shí)間,能解決時(shí)區(qū)問題。


3. 直接是Date類型是數(shù)據(jù)格式,存儲年月日。


在數(shù)據(jù)庫里頭的展示格式如圖所示:



Java代碼里頭獲取的時(shí)間如圖所示:


1.TIMESTAMP格式


afaUser.setLastestLogin(new Date());

2.時(shí)間毫秒級


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();

? ? }

 格式化注意由于數(shù)據(jù)庫是Date類型,所以無論怎么格式化,還是2017/12/21這種格式?!  ?/p>



在往前端傳值的時(shí)候,若是TIMESTAMP類型,前端獲取的是long類型的時(shí)間戳,這時(shí)候就要進(jìn)行格式轉(zhuǎn)換。所以這種格式比較麻煩,盡量少用。


一、在實(shí)體類中進(jìn)行轉(zhuǎn)換


? ? @Column(name = "LASTEST_LOGIN")

? ? private Date lastestLogin;? ? ? ? //返回前端會是毫秒時(shí)間戳。

? ?

? ? @Transient

? ? private String lastLoginDate;? ? ? //新建一個(gè)字段,將lastestLogin格式轉(zhuǎn)化所需要的時(shí)間格式。

? ?

? ? 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進(jìn)行轉(zhuǎn)換


<script language="javascript">?

? ? //擴(kuò)展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;

? ? }

? ? /**?

? ? *轉(zhuǎn)換日期對象為日期字符串?

? ? * @param date 日期對象?

? ? * @param isFull 是否為完整的日期數(shù)據(jù),?

? ? *? ? ? ? ? ? ? 為true時(shí), 格式如"2000-03-05 01:05:04"?

? ? *? ? ? ? ? ? ? 為false時(shí), 格式如 "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);

? ? }

? ? /**?

? ? *轉(zhuǎn)換當(dāng)前日期對象為日期字符串?

? ? * @param date 日期對象?

? ? * @param isFull 是否為完整的日期數(shù)據(jù),?

? ? *? ? ? ? ? ? ? 為true時(shí), 格式如"2000-03-05 01:05:04"?

? ? *? ? ? ? ? ? ? 為false時(shí), 格式如 "2000-03-05"?

? ? * @return 符合要求的日期字符串?

? ? */?


? ? function getSmpFormatNowDate(isFull) {

? ? ? ? return getSmpFormatDate(new Date(), isFull);

? ? }

? ? /**?

? ? *轉(zhuǎn)換long值為日期字符串?

? ? * @param l long值?

? ? * @param isFull 是否為完整的日期數(shù)據(jù),?

? ? *? ? ? ? ? ? ? 為true時(shí), 格式如"2000-03-05 01:05:04"?

? ? *? ? ? ? ? ? ? 為false時(shí), 格式如 "2000-03-05"?

? ? * @return 符合要求的日期字符串?

? ? */?


? ? function getSmpFormatDateByLong(l, isFull) {

? ? ? ? return getSmpFormatDate(new Date(l), isFull);

? ? }

? ? /**?

? ? *轉(zhuǎn)換long值為日期字符串?

? ? * @param l long值?

? ? * @param pattern 格式字符串,例如:yyyy-MM-dd hh:mm:ss?

? ? * @return 符合要求的日期字符串?

? ? */?


? ? function getFormatDateByLong(l, pattern) {

? ? ? ? return getFormatDate(new Date(l), pattern);

? ? }

? ? /**?

? ? *轉(zhuǎn)換日期對象為日期字符串?

? ? * @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>

所以在實(shí)際使用過程中可以使用Date或者事件發(fā)生的時(shí)間毫秒值,少用TIMESTAMP類型進(jìn)行數(shù)據(jù)的存儲。



import java.text.*;

import java.util.Date;


/**

? SimpleDateFormat函數(shù)語法:

?

? G 年代標(biāo)志符

? y 年

? M 月

? d 日

? h 時(shí) 在上午或下午 (1~12)

? H 時(shí) 在一天中 (0~23)

? m 分

? s 秒

? S 毫秒

? E 星期

? D 一年中的第幾天

? F 一月中第幾個(gè)星期幾

? w 一年中第幾個(gè)星期

? W 一月中第幾個(gè)星期

? a 上午 / 下午 標(biāo)記符

? k 時(shí) 在一天中 (1~24)

? K 時(shí) 在上午或下午 (0~11)

? z 時(shí)區(qū)

*/

public class FormatDateTime {


? ? public static void main(String[] args) {

? ? ? ? SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒");

? ? ? ? SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");

? ? ? ? SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等價(jià)于now.toLocaleString()

? ? ? ? SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH時(shí)mm分ss秒 E ");

? ? ? ? SimpleDateFormat myFmt4=new SimpleDateFormat(

? ? ? ? ? ? ? ? "一年中的第 D 天 一年中第w個(gè)星期 一月中第W個(gè)星期 在一天中k時(shí) z時(shí)區(qū)");

? ? ? ? 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時(shí)24分27秒

04/12/16 17:24

2004-12-16 17:24:27

2004年12月16日 17時(shí)24分27秒 星期四

一年中的第 351 天 一年中第51個(gè)星期 一月中第3個(gè)星期 在一天中17時(shí) CST時(shí)區(qū)

16 Dec 2004 09:24:27 GMT

2004-12-16 17:24:27

Thu Dec 16 17:24:27 CST 2004

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評論 19 139
  • Android(安卓)時(shí)間戳和日期之間的轉(zhuǎn)化 https://blog.csdn.net/xiaocheng229...
    壓抑的內(nèi)心閱讀 842評論 0 0
  • OH經(jīng)典卡每日覺察提問 用直覺 1. 你看到了什么? 看到了圖書館,學(xué)生們在讀書。 2. 焦點(diǎn)是哪部分? 是書,第...
    斯慶催眠圖卡療愈師閱讀 162評論 0 0
  • 清明陵園祭母親,墓碑如林花海新。 磕頭思母猶在世,撫育兒女操碎心。
    老槐樹閱讀 339評論 1 1
  • 2017-11-14(70/99) 感恩 —— 迪士尼好萊塢酒店舒適可愛的環(huán)境,每個(gè)大人都曾是孩子,卡通的存在就是...
    慢慢花開閱讀 317評論 0 0

友情鏈接更多精彩內(nèi)容