Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * time1.java
  3.  * time1 class declaration maintains the time in 24-hour format.
  4.  *
  5.  * intan
  6.  */
  7. public class time1
  8. {
  9.     private int hour; //0 - 23
  10.     private int minute; //0 - 59
  11.     private int second; //0 - 59
  12.    
  13.     public void settime(int h, int m, int s)
  14.     {
  15.         if ((h >= 0 && h < 24) && (m >= 0 && m < 60) && (s >= 0 && s <60))
  16.         {
  17.             hour = h;
  18.             minute = m;
  19.             second = s;
  20.         } // end if
  21.         else
  22.         throw new IllegalArgumentException("Hour, minute and/or second was out of range");
  23.     } //end method settime
  24.    
  25.     //convert to Sting in universal-time format (HH:MM:SS)
  26.     public String toUniversalString()
  27.     {
  28.         return String.format("%02d:%02d:%02d", hour, minute, second);
  29.     } //end method toUniversalString
  30.    
  31.     //convert to Stirng in standard-time format (HH:MM:SS)
  32.     public String toString()
  33.     {
  34.         return String.format("%d:%02d:%02d %s",
  35.         ((hour == 0 || hour == 12) ? 12 : hour % 12),
  36.         minute, second, ( hour < 12 ? "AM" : "PM"));
  37.     } //end method toString
  38. } //end class time1