Advertisement
GSculerlor

Time1.java

Sep 14th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. public class Time1
  2. {
  3.     public int hour, minute, second;
  4.    
  5.     public void setTime(int h, int m, int s){
  6.         if((h>=0 && h<24) && (m>=0 && m<60) && (s>=0 && s<60)){
  7.             hour = h;
  8.             minute = m;
  9.             second = s;
  10.         }
  11.         else
  12.             throw new IllegalArgumentException("Out of range!");
  13.     }
  14.    
  15.     public String toGlobalFormat(){
  16.         return String.format("%02d:%02d:%02d", hour, minute, second);
  17.     }
  18.    
  19.     public String toAMPMFormat(){
  20.         return String.format("%d:%02d:%02d %s", ((hour==0 || hour==12) ? 12 : hour%12), minute, second, (hour<12 ? "AM" : "PM"));
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement