Advertisement
makispaiktis

2. Time and Date Class

May 15th, 2022 (edited)
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. import java.util.Date;
  2.  
  3. public class DateClass {
  4.  
  5.  
  6.     private int hours;
  7.     private int minutes;
  8.     private int seconds;
  9.  
  10.  
  11.     // GETTERS, SETTERS
  12.     public void setHours(int h){
  13.         this.hours = h;
  14.     }
  15.     public void setMinutes(int m){
  16.         this.minutes = m;
  17.     }
  18.     public void setSeconds(int s){
  19.         this.seconds = s;
  20.     }
  21.     public int getHours(){
  22.         return this.hours;
  23.     }
  24.     public int getMinutes(){
  25.         return this.minutes;
  26.     }
  27.     public int getSeconds(){
  28.         return this.seconds;
  29.     }
  30.  
  31.  
  32.  
  33.     // CONSTRUCTORS
  34.     public DateClass(int h, int m, int s){
  35.         setHours(h);
  36.         setMinutes(m);
  37.         setSeconds(s);
  38.     }
  39.     public DateClass(int h, int m){
  40.         this(h, m, 0);
  41.     }
  42.     public DateClass(int h){
  43.         this(h, 0, 0);
  44.     }
  45.     public DateClass(){
  46.         this(0, 0, 0);
  47.     }
  48.  
  49.  
  50.  
  51.     // METHODS
  52.     public void setTime(int h, int m, int s){
  53.         this.hours = ((h>=0 && h<24) ? h : 0);
  54.         this.minutes = ((m>=0 && m<60) ? m : 0);
  55.         this.seconds = ((s>=0 && s<60) ? s : 0);
  56.     }
  57.  
  58.     public String toMilitary(){
  59.         return String.format("%02d : %02d : %02d", this.hours, this.minutes, this.seconds);
  60.     }
  61.  
  62.     public String toOrdinary(){
  63.         return String.format("%d : %02d : %02d %s", this.hours % 12, this.minutes, this.seconds, (this.hours >= 12 ? "AM" : "PM"));
  64.     }
  65.  
  66.     public static void main(String args[]){
  67.         System.out.println("Checking military and ordinary hours");
  68.         DateClass dateclass = new DateClass();
  69.         String str01 = dateclass.toMilitary();
  70.         String str02 = dateclass.toOrdinary();
  71.         System.out.println(str01);
  72.         System.out.println(str02);
  73.         dateclass.setTime(4, 23, 18);
  74.         String str11 = dateclass.toMilitary();
  75.         String str12 = dateclass.toOrdinary();
  76.         System.out.println(str11);
  77.         System.out.println(str12);
  78.         dateclass.setTime(16, 23, 18);
  79.         String str21 = dateclass.toMilitary();
  80.         String str22 = dateclass.toOrdinary();
  81.         System.out.println(str21);
  82.         System.out.println(str22);
  83.         System.out.println();
  84.         System.out.println();
  85.  
  86.         DateClass d1 = new DateClass();
  87.         DateClass d2 = new DateClass(14);
  88.         DateClass d3 = new DateClass(14, 15);
  89.         DateClass d4 = new DateClass(14, 15, 16);
  90.         System.out.println(d1.toMilitary());
  91.         System.out.println(d2.toMilitary());
  92.         System.out.println(d3.toMilitary());
  93.         System.out.println(d4.toMilitary());
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement