Advertisement
Guest User

Timer.java

a guest
Jun 28th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. public class Timer {
  2.  
  3.     private long startTime;
  4.     private long duration;
  5.  
  6.     /**
  7.      * Constructs a new timer that records the start time of when the timer was created.
  8.      *
  9.      * @param duration the duration in milliseconds until the timer is expired
  10.      */
  11.     public Timer(long duration) {
  12.         this.duration = duration;
  13.         startTime = System.currentTimeMillis();
  14.     }
  15.  
  16.     /**
  17.      * Returns if the current time has surpassed the set duration from the start time.
  18.      *
  19.      * @return true if surpassed, false otherwise
  20.      */
  21.     public boolean isRunning() {
  22.         return System.currentTimeMillis() - startTime < duration;
  23.     }
  24.  
  25.     /**
  26.      * Returns the amount of time remaining in milliseconds before time expires.
  27.      *
  28.      * @return remaining time
  29.      */
  30.     public long remainingTime() {
  31.         return isRunning() ? duration - (System.currentTimeMillis() - startTime) : 0;
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement