Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-03 15:51:58 10月16日 Update

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.

Tags: Precision type time help problem clarity precision code content function character string for this article novice more format knowledge nanosecond industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Docker Shulou Information MySQL