Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of loss of accuracy in java.sql.Timestamp

2025-01-17 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report