Reputation: 3596
long nanoseconds = System.nanoTime();
long microseconds = nanoseconds / 1000;
long miliseconds = microseconds / 1000;
long seconds = miliseconds / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
System.out.println (hours);
When ran at ~11:35 am on my Windows machine, prints 26
. There can't be 26 hours in a day and even if there were, it's not even close to the expected result 11
. Why doesn't this work? (Also I don't want to use any Time Date library, I know there are easier ways out there)
Hope this all formats correctly since I'm on my phone. Thanks everyone!
Upvotes: 0
Views: 3234
Reputation: 2731
From the Oracle Documentation you can read this:
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees are made about how frequently values change. Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.
So you shouldn't use this method.
Upvotes: 4