Leandros
Leandros

Reputation: 16825

Get time of a calendar in nanoseconds

Since the Java doc tell me, not to use System.currentTimeMillis for comparison, I started using System.nanoTime which is fine.
But I ran into some problems, I have to compare events which are in the past.

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

works fine to get the time in milliseconds, but converting it to nanoseconds (by multiplying it with 1000000) is far to inaccurate.

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

Upvotes: 3

Views: 5852

Answers (2)

Luigi
Luigi

Reputation: 8847

The Calendar class in Java doesn't contain nanosecond information. So you can't get that.

You need to store the nanoseconds as long for the event you want to compare later if you need that detail.: you can't do that too, the nanoTime() is not a representation of current time, but you may still store that to evaluate elapsed time of old processes.

Upvotes: 3

drew moore
drew moore

Reputation: 32680

What data type are you using for the multiplication by 1,000,000 ? Perhaps you should use a BigDecimal, which would be accurate enough for you.

Upvotes: 0

Related Questions