Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Timer {
- private long startTime;
- private long duration;
- /**
- * Constructs a new timer that records the start time of when the timer was created.
- *
- * @param duration the duration in milliseconds until the timer is expired
- */
- public Timer(long duration) {
- this.duration = duration;
- startTime = System.currentTimeMillis();
- }
- /**
- * Returns if the current time has surpassed the set duration from the start time.
- *
- * @return true if surpassed, false otherwise
- */
- public boolean isRunning() {
- return System.currentTimeMillis() - startTime < duration;
- }
- /**
- * Returns the amount of time remaining in milliseconds before time expires.
- *
- * @return remaining time
- */
- public long remainingTime() {
- return isRunning() ? duration - (System.currentTimeMillis() - startTime) : 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement