在平時的開發中,會在資料庫中記錄該條記錄相關的操作時間,或業務上要用到的時間格式

正常使用中有以下三種類型:
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