Is system.out of nanoTime wrong?

I am doing private Timestamp timestamp = new Timestamp(System.nanoTime()); in one of my objects when creating the object.

If I use system.out in timestamp, I get the following values:

 2282-08-24 11:25:00.506 2286-04-16 01:47:35.882 

What is wrong here?

System.currentTimeMillis() gives the correct date, but I need extra precision. What could be wrong here?

+4
source share
3 answers

System.nanoTime() is a completely abstract way of measuring time; it has something to do with the number of CPU cycles since starting up the computer. It is completely unrelated to java.util.Date , which uses the time of an era (the number of milliseconds since 1970).

You can compare two different System.nanoTime() values ​​to get very accurate time measurements (theoretically up to 1 nanosecond), but the absolute value, taken by itself, is useless.

+9
source

This method can only be used to measure elapsed time and is not related to any other concept of system or wall time. The return value is nanoseconds from some fixed, but arbitrary start time (possibly in the future, so the values ​​may be negative). The same origin is used by all calls to this method in an instance of the Java virtual machine; other virtual machine instances may use a different origin.

documentation

+1
source

Timestamp expects a new timestamp (miliseconds). If you give it nanoseconds (new timestamp (nanoseconds) β†’ 1000 times more), you will get a timestamp 1000 times in the future, whatever that means. This is the reason because the dates received in year 2282

0
source

Source: https://habr.com/ru/post/1443658/


All Articles