Advertisement
Ramdan51-062

Time class

Sep 14th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. /**
  2.  * Untuk menentukan waktu.
  3.  *
  4.  * @Ramdan
  5.  * @1.0.0
  6.  */
  7. public class Time
  8. {
  9.     // instance variables - replace the example below with your own
  10.     private int hour;
  11.     private int minute;
  12.     private int second;
  13.  
  14.  
  15.     public void setTime(int h, int m, int s)
  16.     {
  17.         // initialise instance variables
  18.         if ((h >= 0 && h < 24) && (m >=0 && m < 60) && (s >=0 && s < 60)){
  19.             hour = h;
  20.             minute = m;
  21.             second = s;
  22.         }
  23.     else
  24.         throw new IllegalArgumentException( "Your time is messed up");
  25.  
  26.    
  27.     }
  28.     public String toUniversalString()
  29.     {
  30.         return String.format ("%02d:%02d:%02d", hour, minute, second);
  31.     }
  32.    public String toString(){
  33.     return String.format("%02d:%02d:%02d %s", ((hour == 0 || hour == 12) ? 12 : hour % 12), minute, second, ( hour < 12 ? "AM" : "PM"));
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement