Class Timer


  • public class Timer
    extends java.lang.Object
    A utility class for managing and tracking elapsed time and performing time-based calculations.
    • Constructor Summary

      Constructors 
      Constructor Description
      Timer()
      Creates a Timer with no initial duration.
      Timer​(long duration)
      Creates a Timer with a specified duration.
      Timer​(long duration, long start)
      Creates a Timer with a specified start time and duration.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long getElapsedTime()
      Calculates the elapsed time since the timer started.
      int getPerHour​(int gained)
      Calculates the hourly rate based on a gained value.
      int getPerHour​(int gained, int startAmount)
      Calculates the hourly rate based on a gained value and an initial start amount.
      long getRemaining()
      Retrieves the remaining time until the timer ends.
      java.lang.String getRemainingTime()
      Formats the remaining time as a string in the format "DD:HH:MM:SS".
      boolean isFinished()
      Checks if the timer has finished (end time reached).
      boolean isRunning()
      Checks if the timer is currently running.
      void reset()
      Resets the timer, initializing it to the current time if it was stopped.
      void restart()
      Restarts the timer by resetting and resuming it.
      void stop()
      Stops the timer, preserving the remaining duration for resumption.
      java.lang.String toString()
      Generates a formatted string of the elapsed time in the format "DD:HH:MM:SS".
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Timer

        public Timer​(long duration)
        Creates a Timer with a specified duration.
        Parameters:
        duration - The duration in milliseconds for which the timer should run.
      • Timer

        public Timer​(long duration,
                     long start)
        Creates a Timer with a specified start time and duration.
        Parameters:
        duration - The duration in milliseconds for which the timer should run.
        start - The custom start time in milliseconds.
      • Timer

        public Timer()
        Creates a Timer with no initial duration.
    • Method Detail

      • getRemaining

        public long getRemaining()
        Retrieves the remaining time until the timer ends.
        Returns:
        The remaining time in milliseconds. Returns 0 if the timer has already ended.
      • isFinished

        public boolean isFinished()
        Checks if the timer has finished (end time reached).
        Returns:
        true if the timer has finished, otherwise false.
      • restart

        public void restart()
        Restarts the timer by resetting and resuming it.
      • reset

        public void reset()
        Resets the timer, initializing it to the current time if it was stopped.
      • stop

        public void stop()
        Stops the timer, preserving the remaining duration for resumption.
      • isRunning

        public boolean isRunning()
        Checks if the timer is currently running.
        Returns:
        true if the timer is running, otherwise false.
      • getElapsedTime

        public long getElapsedTime()
        Calculates the elapsed time since the timer started.
        Returns:
        The elapsed time in milliseconds.
      • getPerHour

        public int getPerHour​(int gained)
        Calculates the hourly rate based on a gained value.
        Parameters:
        gained - The total value gained during the elapsed time.
        Returns:
        The calculated rate per hour.
      • getPerHour

        public int getPerHour​(int gained,
                              int startAmount)
        Calculates the hourly rate based on a gained value and an initial start amount.
        Parameters:
        gained - The total value gained during the elapsed time.
        startAmount - The initial amount at the start of the timer.
        Returns:
        The calculated rate per hour.
      • getRemainingTime

        public java.lang.String getRemainingTime()
        Formats the remaining time as a string in the format "DD:HH:MM:SS".
        Returns:
        The formatted remaining time string.
      • toString

        public java.lang.String toString()
        Generates a formatted string of the elapsed time in the format "DD:HH:MM:SS".
        Overrides:
        toString in class java.lang.Object
        Returns:
        The formatted elapsed time string.