In a Java application I want to be able to take a timestamp at the start of an operation and be able to periodically check how long the operation has been running. The catch is: I do not want to be impacted by the Network Time Protocol moving the clock around, or the admin changing the time, or anything which can abruptly adjust the time of day. I want a monotonically increasing time value. I believe this rules out java.util.Date, Time, and Calendar.
Is there some source of a monotonically increasing timestamp in the JRE?
A monotonic clock is a time source that won't ever jump forward or backward (due to NTP or Daylight Savings Time updates).
nanoTime() is monotonic (if the underlying system supports it).
Have you considered using System.nanoTime()?
It is only meaningful in working out elapsed time between two events. Since the documentation states that it is not related to any system or wall time I believe it could be used in your sitatuion.
JodaTime has a class to do this. It is called Duration.
"They have no chronology or time zone, and consist solely of the millisecond duration."
Also you could set your Date stamp to Greenwich time and the Network Time Protocol should not be an issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With