Nanosecond time in Java, do not use Java util. Date

I need as little garbage collection (GC) as possible, very accurate and accurate time, ideally once a day System. Currenttimemillis() is not accurate enough, system Nanotime() is not an accurate time source The only thing that can provide what I want is Java util. Date. Gettime(), but it is not a static method, so every time I need a precise and accurate time, I must create a new date object, which will cause more GC to be triggered frequently

Who knows how the date class gets an accurate and precise time? I want to take the date method and customize it to minimize object creation To summarize my questions, see the following:

long nanosSinceEpoch;
nanosSinceEpoch = System.currentTimeMillis(); // Not precise
nanosSinceEpoch = System.nanoTime();          // Not accurate
nanosSinceEpoch = new Date().getTime();       // Too many objects

Edit:

I'm not sure why I think system Currenttimemillis() and new date() Gettime () is different, but it turns out they are the same Unfortunately, this did not solve my problem

In addition, Java time. Getnano () in instant seems to have a resolution of only milliseconds

Solution

Instant. Now

Java 9 brings a new clock implementation, which can capture the current time in a more refined way than the milliseconds function of Java 8 clock

Instant instant = Instant.Now() ;

Let me be clear: the instant class in all versions of Java can save a value in nanoseconds However, the current capture time is limited to milliseconds in version 8

You should study the hardware clock function of the current computer device I believe you will find that the current traditional hardware can not accurately track nanosecond time

Using Oracle JDK 9.0.0 on MacOS Sierra At 4 o'clock, I saw the current time captured in microseconds, six digits after the decimal point

No, accuracy is not a problem The problem is system Nanotime() is designed to track elapsed time, not the current date and time This function only tracks the nanosecond count since any unrecorded starting point

Moreover, as mentioned above, we are not talking about increasing single nanosecond, because the current traditional computer hardware does not have

About Java time

java. The time framework is built into Java 8 and later These courses replace the troublesome old legacy date time courses, such as Java util. Date, calendar and & simpledateformat

The joda time project is now maintenance mode. It is recommended to migrate to Java Time course

For more information, see Oracle tutorial And search stack overflow for many examples and instructions Specification is @ L_ 502_ 14@.

You can exchange Java. Net directly with the database Time object Use a jdbc driver that conforms to JDBC 4.2 or later No strings, no Java sql.* Class

Where to get Java Time class?

>Java se 8, Java se 9 and later

>Built in. > Some standard Java APIs with bundled implementations. > Java 9 has added some small functions and fixes

>Java se 6 and Java se 7

>Most Java The time function was reverse ported to Java 6& July at threeten backup

> Android

>Updated version of Android bundled Java Implementation of time class. > For early Android (< 26), threeten ABP project adopted threeten backup (as described above) See how to use threetenabp

The threeten extra project extends Java. Net with other classes time. The project is likely to be added to Java in the future Time's proving ground You can find some useful classes here, such as interval, yearweek, yearquarter and more

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>