Advertisement
Guest User

ClockTime

a guest
Sep 18th, 2019
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. /*Java Time Practice
  2.   Nikodemus Siahaan
  3.   05111840000151
  4.   PBO A
  5.  */
  6. public class ClockTime
  7. {
  8.         private int hour; // 0-23
  9.         private int minute; // 0-59
  10.         private int second; // 0-59
  11.        
  12.         public ClockTime(int hour, int minute, int second)
  13.         {
  14.             if((hour >= 0 && hour < 24) && (minute >= 0 && minute < 60) && (second >= 0 && second < 60))
  15.                 { this.hour = hour;
  16.                   this.minute = minute;
  17.                   this.second = second;
  18.                 }
  19.                 else {
  20.                     this.hour=0;
  21.                     this.minute=0;
  22.                     this.second=0;
  23.                 }
  24.             }
  25.         public String to24hourformat()
  26.     {
  27.         return String.format ("%02d:%02d:%02d", hour, minute, second);
  28.     }
  29.         public String to12hourformat()
  30.     {
  31.                 String AMorPM;
  32.                 if(hour < 12) {
  33.                     AMorPM = "AM";
  34.                 }
  35.                 else{ AMorPM = "PM";
  36.                 }
  37.                
  38.                 if(hour == 0 || hour == 12)
  39.                 { hour = 12;
  40.                 }
  41.                 else
  42.                 { hour = hour % 12;
  43.                 }  
  44.                 return String.format ("%d:%02d:%02d %s", hour, minute, second, AMorPM);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement