Get calendar time in nanoseconds

Since the Java doc tells me not to use System.currentTimeMillis

for comparison, I started using System.nanoTime

which is good.
But I ran into some problems, I have to compare events that were in the past.

Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, xyz);
cal.getTimeMillis();

      

works great to get the time in milliseconds

, but converting it to nanoseconds

(by multiplying it by 1,000,000) is far from accurate.

How can I get the time of an event in the past in milliseconds?

+3


source to share


2 answers


A class Calendar

in Java does not contain information about nanoseconds. Therefore you cannot understand it.



You need to store the nanoseconds as long

for the event you want to compare later if you want this detail.
: you can't do that either, nanoTime () is not a representation of the current time, but you can still save this to estimate the elapsed time of old processes.

+3


source


What data type are you using to multiply by 1,000,000? Perhaps you should use BigDecimal, which is accurate enough for you.



0


source







All Articles