javabook
Class Clock

java.lang.Object
  |
  +--javabook.Clock

public class Clock
extends java.lang.Object

This class supports some basic clock functions such as reading the current time, getting today's date, and stop watch function. This class also supports a convenient function to pause the program execution.

This class is provided as freeware. You are free to use as provided or modify to your heart's content. But you use it at your own risk. No implied or explicit warranty is given.


Field Summary
static int LONG_FORMAT
          Designates the long format for date
static int SHORT_FORMAT
          Designates the short format for date
static int TIME_12HR_FORMAT
          Designates the 12-hr format for time
static int TIME_24HR_FORMAT
          Designates the 24-hr format for time
 
Constructor Summary
Clock()
          Default constructor.
 
Method Summary
 java.lang.String getCurrentDate()
          Returns the current (today's) date in the mm/dd/yyyy format.
 java.lang.String getCurrentDate(int format)
          Returns the current (today's) date in short or long format.
 java.lang.String getCurrentTime()
          Returns the current time in the 12-hr format.
 java.lang.String getCurrentTime(int format)
          Returns the current time in 24-hr or 12-hr format.
 double getElapsedTime()
          Returns the elapsed time of the stop watch function.
 double getTotalElapsedTime()
          Returns the total elapsed time of the stop watch function.
 void pause(double duration)
          Pauses the program execution.
 void reset()
          Resets the stop watch to its initial state.
 void start()
          Starts the clock's stop watch.
 void stop()
          Stops the clock's stop watch.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LONG_FORMAT

public static final int LONG_FORMAT
Designates the long format for date

SHORT_FORMAT

public static final int SHORT_FORMAT
Designates the short format for date

TIME_12HR_FORMAT

public static final int TIME_12HR_FORMAT
Designates the 12-hr format for time

TIME_24HR_FORMAT

public static final int TIME_24HR_FORMAT
Designates the 24-hr format for time
Constructor Detail

Clock

public Clock()
Default constructor.
Method Detail

getCurrentDate

public java.lang.String getCurrentDate()
Returns the current (today's) date in the mm/dd/yyyy format.
Returns:
today's date in either the mm/dd/yyyy format

getCurrentDate

public java.lang.String getCurrentDate(int format)
Returns the current (today's) date in short or long format. The short format has the standard mm/dd/yyyy. The long format spells out the month name.
Parameters:
format - specify LONG_FORMAT or SHORT_FORMAT
Returns:
today's date in either short or long format

getCurrentTime

public java.lang.String getCurrentTime()
Returns the current time in the 12-hr format.
Returns:
current time in the 12-hr format

getCurrentTime

public java.lang.String getCurrentTime(int format)
Returns the current time in 24-hr or 12-hr format. The 12-hr format will include the AM/PM designation.
Parameters:
format - specify TIME_12HR_FORMAT or TIME_24HR_FORMAT
Returns:
current time in either 12-hr or 24-hr format

getElapsedTime

public double getElapsedTime()
Returns the elapsed time of the stop watch function. The elapsed time is the time interval between the most recent start and stop. If the stop watch is still running, then this method returns the elapsed time from the most recent call to the start method until this method is called. The stop watch will keep on running.
Returns:
elapsed time (unit is seconds)

getTotalElapsedTime

public double getTotalElapsedTime()
Returns the total elapsed time of the stop watch function. The total elapsed time is the time interval between the first start and the last stop. If the stop watch is still running, then this method returns the elapsed time from the first time the stop watch is started till the time this method is called. The stop watch will keep on running.
Returns:
elapsed time (unit is seconds)

pause

public void pause(double duration)
Pauses the program execution. If the clock is running, then the timer keeps on counting while the program execution is paused. If the clock is not running, then this method simply pauses the program execution.
Parameters:
duration - the amount to time (in seconds) to pause the program execution

reset

public void reset()
Resets the stop watch to its initial state.

start

public void start()
Starts the clock's stop watch. If the stop watch is already running, then this method is imply ignored.

stop

public void stop()
Stops the clock's stop watch. If the stop watch is not running, then this method is simply ignored.