new Date()與new Timestamp()輸出對比
由於我的后台的時間格式:yyyy-MM-dd HH:mm:ss,所以上傳的數據會忽略毫秒,由於我不知道new Date()會有毫秒上的誤差,畢竟單純地輸出new Date() 會顯示Sun Jan 12 12:02:56 CST 2020,看不出區別。
當我們getTime()的時候,會發現它是記錄毫秒的
new Date() ---------------------------- Sun Jan 12 12:02:56 CST 2020
new Timestamp(System.currentTimeMillis()) -- 2020-01-12 12:02:56.339
new Date().getTime() --------------------------------- 1578801776339
new Timestamp(System.currentTimeMillis()).getTime() -- 1578801776339
如果本地數據不做處理的話,會與本地數據發生沖突,除非你在上傳數據后再獲取數據,保持本地數據與服務器數據相同。
Timestamp.valueof()忽略毫秒
通過獲取格式化Date為yyyy-MM-dd HH:mm:ss實現
/**
* 輸出如下
* nowDateTime() --------------------------- 2020-01-12 12:02:56.0
* nowDateTime().getTime() ------------------------- 1578801776000
*/
public static Date nowDateTime(){
return Timestamp.valueOf(
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE).format(new Date())
);
}