Advertisement
kuchuz

PBO-C 1 : Time2()

Oct 15th, 2020
1,932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. public class Time2{
  2.     private int hour,minute,second;
  3.     public Time2() {
  4.         this(0, 0, 0);
  5.     }
  6.     public Time2(int h) {
  7.         this(h, 0, 0);
  8.     }
  9.     public Time2(int h, int m) {
  10.         this(h, m, 0);
  11.     }
  12.     public Time2(int h, int m, int s) {
  13.         setTime(h, m, s);
  14.     }
  15.     public Time2(Time2 time) {
  16.         this(time.getHour(), time.getMinute(), time.getSecond());
  17.     }
  18.     public void setTime(int h, int m, int s) {
  19.         setHour(h);
  20.         setMinute(m);
  21.         setSecond(s);
  22.     }
  23.     public void setHour(int h) {
  24.         if (h >= 0 && h < 24)
  25.             hour = h;
  26.         else
  27.             throw new IllegalArgumentException("hour must be 0-23");
  28.     }
  29.     public void setMinute(int m) {
  30.         if (m >= 0 && m < 60)
  31.             minute = m;
  32.         else
  33.             throw new IllegalArgumentException("minute must be 0-59");
  34.     }
  35.     public void setSecond(int s) {
  36.         if (s >= 0 && s < 60)
  37.             second = ((s >= 0 & s < 60) ? s : 0);
  38.         else
  39.             throw new IllegalArgumentException("second must be 0-59");
  40.     }
  41.     public int getHour() {
  42.         return hour;
  43.     }
  44.     public int getMinute() {
  45.         return minute;
  46.     }
  47.     public int getSecond() {
  48.         return second;
  49.     }
  50.     public String toUniversalString() {
  51.         return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
  52.     }
  53.     public String toString() {
  54.         return String.format("%d:%02d:%02d:%s", ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement