The constructor of java.sql.Timestamp looks like this:
public Timestamp(long time) { super((time/1000)*1000); nanos = (int)((time%1000) * 1000000); if (nanos < 0) { nanos = 1000000000 + nanos; super.setTime(((time/1000)-1)*1000); } }
It basically takes time in milliseconds and then extracts the last 3 digits and makes it a sediment. So, for the millisecond value of 1304135631 421 , I get Timestamp.getnanos () as 421000000 . This is a simple calculation (adding 6 zeros at the end) ... does not seem optimal.
A better way could be the Timestamp constructor, which takes time in nanoseconds and then calculates the nanosecond value.
If you run the program below, you will see the difference between the actual nanoseconds and the one returned by the Timestamp method for calculating nanoscopes.
long a = System.currentTimeMillis(); for(;;){ long b = System.currentTimeMillis(); Timestamp tm = new Timestamp(System.currentTimeMillis()); System.out.println(tm.getTime()); System.out.println(tm.getNanos()); System.out.println("This is actual nanos" + System.nanoTime()%1000000000); System.out.println("--------------------------"); if(ba >= 1) break; }
So, the whole discussion about Timestamp, which says that it stores time to nanoseconds, does not seem so right. Not?
source share