Timer
utility class for things like timing how long a task takes, etc? Most of the searches on Google return results for timers that schedule threads and tasks, which is not what I want.
Using Instant. Instant class can be used to record event time-stamps in the application. It has now() method that obtains the current instant from the system clock. We can convert this instant to the total number of milliseconds using toEpochMilli() method. Please note that Instant.
The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.
The time functions can be accessed from the java. util. Date class. This represents an instance of time with millisecond precision.
There is always the old-fashioned way:
long startTime = System.nanoTime(); methodToTime(); long endTime = System.nanoTime(); long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.
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