解決java.sql.Timestamp丟失精度的問(wèn)題
java.sql.Timestamp丟失精度
Timestamp的構(gòu)造函數(shù)Timestamp(long time) 會(huì)丟失納秒部分的精度
需要重新補(bǔ)償
Timestamp t1 = Timestamp.valueOf("2019-12-13 15:19:53.2202080");
Timestamp t2 = new Timestamp(1576250393220208000L / 1000000L);
t2.setNanos((int) (1576250393220208000L % 1000000000L));
java.sql.Timestamp類的使用
Timestamp 可以精確到小數(shù)秒 一般存儲(chǔ)的格式:2016-12-18 11:05:36.531
Timestamp 可以獲取當(dāng)前時(shí)間,也可以把字符串裝換成Timestamp類型
1. 獲取當(dāng)前時(shí)間
@Test
public void getCurrentTime(){
//第一種
Date date = new Date();
Timestamp currentTime1 = new Timestamp(date.getTime());
System.out.println("currentTime1:"+currentTime1);
//第二種
Timestamp currentTime2 = new Timestamp(System.currentTimeMillis());
System.out.println("currentTime2:"+currentTime2);
}
2.String類型轉(zhuǎn)換為Timestamp
@Test
public void stringConvertTimestamp(){
String timeStr = "2016-12-18 11:16:33.706";
Timestamp ts = Timestamp.valueOf(timeStr);
System.out.println(ts);
}
3.Timestamp轉(zhuǎn)換為String類型

@Test
public void timestampConvertString(){
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
String timeStr = sdf.format(currentTime);
System.out.println(timeStr);
}
整個(gè)演示類的代碼:
package com.demo;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
public class DemoTimestamp {
@Test
public void getCurrentTime(){
//第一種
Date date = new Date();
Timestamp currentTime1 = new Timestamp(date.getTime());
System.out.println("currentTime1:"+currentTime1);
//第二種
Timestamp currentTime2 = new Timestamp(System.currentTimeMillis());
System.out.println("currentTime2:"+currentTime2);
}
@Test
public void stringConvertTimestamp(){
String timeStr = "2016-12-18 11:16:33.706";
Timestamp ts = Timestamp.valueOf(timeStr);
System.out.println(ts);
}
@Test
public void timestampConvertString(){
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
Timestamp currentTime = new Timestamp(System.currentTimeMillis());
String timeStr = sdf.format(currentTime);
System.out.println(timeStr);
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java常用的八種排序算法及代碼實(shí)現(xiàn)+圖解
這篇文章主要介紹了Java常用的八種排序算法及代碼實(shí)現(xiàn),在Java的時(shí)候,對(duì)于排序的應(yīng)用需要熟練的掌握,這樣才能夠確保Java學(xué)習(xí)時(shí)候能夠有扎實(shí)的基礎(chǔ)能力。那Java有哪些排序算法呢?本文小編就來(lái)詳細(xì)說(shuō)說(shuō)Java經(jīng)典的8種排序算法,需要的朋友可以參考一下2021-12-12
Java實(shí)現(xiàn)將彩色PDF轉(zhuǎn)為灰度PDF的示例代碼
本文以Java代碼為例介紹如何實(shí)現(xiàn)將彩色PDF文件轉(zhuǎn)為灰度(黑白)的PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-03-03
Java ConcurrentModificationException異常解決案例詳解
這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
SpringCloud Eureka實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)
Eureka是一種基于REST(具像狀態(tài)傳輸)的服務(wù),主要用于AWS云中定位服務(wù),以實(shí)現(xiàn)中間層服務(wù)器的負(fù)載平衡和故障轉(zhuǎn)移。本文記錄一個(gè)簡(jiǎn)單的服務(wù)注冊(cè)與發(fā)現(xiàn)實(shí)例。感興趣的小伙伴們可以參考一下2019-01-01
MyBatis-Plus 樂(lè)觀鎖的具體實(shí)現(xiàn)
MyBatis-Plus 的樂(lè)觀鎖通過(guò)簡(jiǎn)單的配置和注解,可以輕松實(shí)現(xiàn)高并發(fā)場(chǎng)景下的數(shù)據(jù)并發(fā)控制,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
Spring Security其它權(quán)限校驗(yàn)方式&自定義權(quán)限校驗(yàn)方式
這篇文章主要介紹了Spring Security其它權(quán)限校驗(yàn)方式&自定義權(quán)限校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
詳解JavaWeb過(guò)濾器 Filter問(wèn)題解決
過(guò)濾器就是對(duì)事物進(jìn)行過(guò)濾的,在Web中的過(guò)濾器,當(dāng)然就是對(duì)請(qǐng)求進(jìn)行過(guò)濾,我們使用過(guò)濾器,就可以對(duì)請(qǐng)求進(jìn)行攔截,然后做相應(yīng)的處理,實(shí)現(xiàn)許多特殊功能,今天主要給大家講解JavaWeb過(guò)濾器 Filter問(wèn)題解決,感興趣的朋友一起看看吧2022-10-10
Springboot通過(guò)aop實(shí)現(xiàn)事務(wù)控制過(guò)程解析
這篇文章主要介紹了Springboot通過(guò)aop實(shí)現(xiàn)事務(wù)控制過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

