public final class Timer
extends java.lang.Object
Timer
represents a timer that can be used to measure the time that an activity has taken so far.
When you create a new Timer
instance, it starts immediately. If you want to restart it, consider calling restart()
.
The default methods of this class uses System.currentTimeMillis()
in the calculations. But all default methods have an overloaded method that requires a long milliseconds
parameter. Prefer to use these overloaded methods whenever
you need to call multiple methods at the same "time". Otherwise, the results you get may not end up as you would expect.
Constructor and Description |
---|
Timer()
Constructs a new
Timer instance and starts it. |
Timer(long milliseconds)
Constructs a new
Timer instance and starts it. |
Modifier and Type | Method and Description |
---|---|
long |
getHours()
Returns the hours that have passed between the time at which this
Timer instance was started or restarted and now. |
long |
getHours(long milliseconds)
Returns the hours that have passed between the time at which this
Timer instance was started or restarted and milliseconds . |
long |
getMinutes()
Returns the minutes that have passed between the time at which this
Timer instance was started or restarted and now. |
long |
getMinutes(long milliseconds)
Returns the minutes that have passed between the time at which this
Timer instance was started or restarted and milliseconds . |
long |
getMinutesRemaining()
Returns the minutes that have passed between the time at which this
Timer instance was started or restarted and now, excluding the minutes that are represented by full hours. |
long |
getMinutesRemaining(long milliseconds)
Returns the minutes that have passed between the time at which this
Timer instance was started or restarted and milliseconds , excluding the minutes that are represented by full hours. |
long |
getSeconds()
Returns the seconds that have passed between the time at which this
Timer instance was started or restarted and now. |
long |
getSeconds(long milliseconds)
Returns the seconds that have passed between the time at which this
Timer instance was started or restarted and milliseconds . |
long |
getSecondsRemaining()
Returns the seconds that have passed between the time at which this
Timer instance was started or restarted and now, excluding the seconds that are represented by full hours and minutes. |
long |
getSecondsRemaining(long milliseconds)
Returns the seconds that have passed between the time at which this
Timer instance was started or restarted and milliseconds , excluding the seconds that are represented by full hours and minutes. |
java.lang.String |
getTime()
Returns a
String with the hours, minutes and seconds that have passed between the time at which this Timer instance was started or restarted and now. |
java.lang.String |
getTime(long milliseconds)
Returns a
String with the hours, minutes and seconds that have passed between the time at which this Timer instance was started or restarted and milliseconds . |
void |
restart()
Restarts this
Timer instance. |
void |
restart(long milliseconds)
Restarts this
Timer instance. |
public Timer()
Timer
instance and starts it.
Calling this constructor is equivalent to new Timer(System.currentTimeMillis())
.
public Timer(long milliseconds)
Timer
instance and starts it.milliseconds
- the milliseconds to measure frompublic java.lang.String getTime()
String
with the hours, minutes and seconds that have passed between the time at which this Timer
instance was started or restarted and now.
Calling this method is equivalent to getTime(System.currentTimeMillis())
.
String
with the hours, minutes and seconds that have passed between the time at which this Timer
instance was started or restarted and nowpublic java.lang.String getTime(long milliseconds)
String
with the hours, minutes and seconds that have passed between the time at which this Timer
instance was started or restarted and milliseconds
.
This method uses getHours(milliseconds)
, getMinutesRemaining(milliseconds)
and getSecondsRemaining(milliseconds)
to get the hours, minutes and seconds, respectively.
milliseconds
- the milliseconds to measure toString
with the hours, minutes and seconds that have passed between the time at which this Timer
instance was started or restarted and milliseconds
public long getHours()
Timer
instance was started or restarted and now.
Calling this method is equivalent to getHours(System.currentTimeMillis())
.
Timer
instance was started or restarted and nowpublic long getHours(long milliseconds)
Timer
instance was started or restarted and milliseconds
.milliseconds
- the milliseconds to measure toTimer
instance was started or restarted and milliseconds
public long getMinutes()
Timer
instance was started or restarted and now.
Calling this method is equivalent to getMinutes(System.currentTimeMillis())
.
Timer
instance was started or restarted and nowpublic long getMinutes(long milliseconds)
Timer
instance was started or restarted and milliseconds
.milliseconds
- the milliseconds to measure toTimer
instance was started or restarted and milliseconds
public long getMinutesRemaining()
Timer
instance was started or restarted and now, excluding the minutes that are represented by full hours.
Calling this method is equivalent to getMinutesRemaining(System.currentTimeMillis())
.
Timer
instance was started or restarted and now, excluding the minutes that are represented by full hourspublic long getMinutesRemaining(long milliseconds)
Timer
instance was started or restarted and milliseconds
, excluding the minutes that are represented by full hours.milliseconds
- the milliseconds to measure toTimer
instance was started or restarted and milliseconds
, excluding the minutes that are represented by full hourspublic long getSeconds()
Timer
instance was started or restarted and now.
Calling this method is equivalent to getSeconds(System.currentTimeMillis())
.
Timer
instance was started or restarted and nowpublic long getSeconds(long milliseconds)
Timer
instance was started or restarted and milliseconds
.milliseconds
- the milliseconds to measure toTimer
instance was started or restarted and milliseconds
public long getSecondsRemaining()
Timer
instance was started or restarted and now, excluding the seconds that are represented by full hours and minutes.
Calling this method is equivalent to getSecondsRemaining(System.currentTimeMillis())
.
Timer
instance was started or restarted and now, excluding the seconds that are represented by full hours and minutespublic long getSecondsRemaining(long milliseconds)
Timer
instance was started or restarted and milliseconds
, excluding the seconds that are represented by full hours and minutes.milliseconds
- the milliseconds to measure toTimer
instance was started or restarted and milliseconds
, excluding the seconds that are represented by full hours and minutespublic void restart()
Timer
instance.
Calling this method is equivalent to restart(System.currentTimeMillis())
.
public void restart(long milliseconds)
Timer
instance.milliseconds
- the milliseconds to measure from