Advertisement
apl-mhd

Time1

Nov 8th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Time1 {
  4.  
  5.  
  6.     private int hour;
  7.     private  int minute;
  8.     private int second;
  9.  
  10.     public void setTime(int hour, int minute, int second){
  11.  
  12.         if(hour<0 || hour>24 || minute<0 || minute > 59 || second<0 || second>59){
  13.  
  14.             throw new IllegalArgumentException("Hour, minute, or second out of range");
  15.         }
  16.  
  17.         this.hour = hour;
  18.         this.minute = minute;
  19.         this.second = second;
  20.  
  21.  
  22.  
  23.  
  24.     }
  25.  
  26.  
  27.     public  String toUniversalString(){
  28.  
  29.         return String.format("%d:%d:%d",hour,minute,second);
  30.     }
  31.  
  32.     public  String toString(){
  33.  
  34.         return String.format("%d:%d:%d %s",
  35.                 ((hour==0 || hour==12)?12:hour%12),minute,second,(hour<12? "AM":"PM"));
  36.     }
  37.  
  38.  
  39.     public void show(Time1 ob){
  40.  
  41.         System.out.printf("%s %s %s",ob.hour,ob.minute,ob.second);
  42.  
  43.  
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement