In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to solve the problem of java.sql.Timestamp loss of precision, many novices are not very clear, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Java.sql.Timestamp loses precision
The constructor Timestamp (long time) of Timestamp loses the precision of the nanosecond part
Need to re-compensate Timestamp T1 = Timestamp.valueOf ("2019-12-13 1515 Timestamp T2 = new Timestamp (1576250393220208000L / 1000000L); t2.setNanos ((int) (15762503932208000L% 10000000L)); use of the java.sql.Timestamp class
Timestamp can be accurate to the format of fractional seconds: 2016-12-18 11: 05VR 36.531
Timestamp can get the current time, or you can change the string to Timestamp type.
1. Get the current time
@ Testpublic void getCurrentTime () {/ / first Date date = new Date (); Timestamp currentTime1 = new Timestamp (date.getTime ()); System.out.println ("currentTime1:" + currentTime1); / / second Timestamp currentTime2 = new Timestamp (System.currentTimeMillis ()); System.out.println ("currentTime2:" + currentTime2);} 2.String type converted to Timestamp
@ Testpublic void stringConvertTimestamp () {String timeStr = "2016-12-18 1115 11Timestamp ts 33.706"; Timestamp ts = Timestamp.valueOf (timeStr); System.out.println (ts);} 3.Timestamp converts to String type
@ Testpublic 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);}
The code for the entire demo class:
Package com.demo; import java.sql.Timestimport java.text.SimpleDateFormat;import java.util.Date; import org.junit.Test; public class DemoTimestamp {@ Test public void getCurrentTime () {/ / first type Date date = new Date (); Timestamp currentTime1 = new Timestamp (date.getTime ()); System.out.println ("currentTime1:" + currentTime1) / / the second kind of Timestamp currentTime2 = new Timestamp (System.currentTimeMillis ()); System.out.println ("currentTime2:" + currentTime2);} @ Test public void stringConvertTimestamp () {String timeStr = "2016-12-18 11-11 System.currentTimeMillis 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) }} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.