1+ import java .util .Scanner ;
2+
3+ class Time2 {
4+
5+ int hr ,min ,sec ;
6+
7+ public Time2 ()
8+ {
9+ // Default
10+ }
11+
12+ public Time2 (int hr , int min , int sec )
13+ {
14+
15+ this .hr = hr ;
16+ this .min = min ;
17+ this .sec = sec ;
18+
19+ }
20+
21+ public void sethr (int hr )
22+ {
23+ this .hr = hr ;
24+ }
25+ public int gethr ()
26+ {
27+ return hr ;
28+ }
29+ public int getmin ()
30+ {
31+ return min ;
32+ }
33+ public void setmin (int min )
34+ {
35+ this .min = min ;
36+ }
37+ public void setsec (int sec )
38+ {
39+ this .sec = sec ;
40+ }
41+ public int getsec ()
42+ {
43+ return sec ;
44+ }
45+
46+ void get (){
47+
48+ gethr ();
49+ getmin ();
50+ getsec ();
51+
52+ }
53+ void display ()
54+ {
55+ if ((hr < 12 && hr >=0 ) || hr > 23 ) {
56+ System .out .println ("\n Time is : " +hr + " hours " + " : " +min + " minutes " + " : " +sec + " seconds " + " AM\n " );
57+ }
58+ else {
59+ System .out .println ("\n Time is : " +hr + " hours " + " : " +min + " minutes " + " : " +sec + " seconds " + " PM\n " );
60+ }
61+ }
62+ public void difer (Time2 T )
63+ {
64+ double h ;
65+ if (this .gethr () < T .gethr ()) {
66+ h = (T .gethr () - this .gethr ());
67+ }
68+ else
69+ {
70+ h = this .gethr () - T .gethr ();
71+ }
72+
73+ double m ;
74+ if (this .getmin () < T .getmin ()) {
75+ m = (T .getmin () - this .getmin ());
76+ }
77+ else
78+ {
79+ m = this .getmin () - T .getmin ();
80+ }
81+
82+ double s ;
83+ if (this .getsec () < T .getsec ()) {
84+ s = (T .getsec () - this .getsec ());
85+ }
86+ else
87+ {
88+ s = this .getsec () - T .getsec ();
89+ }
90+
91+ System .out .println ("Time difference is : " +h + " Hours " + " : " +m + " Minutes " + " : " + s + " Seconds " );
92+
93+ }
94+
95+ }
96+
97+ public class time
98+ {
99+ public static void main (String []args ){
100+ Scanner sc = new Scanner (System .in );
101+ System .out .println ("Enter hrs, min and secs : " );
102+ int x1 = sc .nextInt ();
103+ int y1 = sc .nextInt ();
104+ int z1 = sc .nextInt ();
105+
106+ Time2 t2 = new Time2 ();
107+
108+
109+ t2 .sethr (x1 );
110+ t2 .setmin (y1 );
111+ t2 .setsec (z1 );
112+ t2 .display ();
113+
114+ System .out .println ("Enter hrs, min and secs : " );
115+ int x2 = sc .nextInt ();
116+ int y2 = sc .nextInt ();
117+ int z2 = sc .nextInt ();
118+
119+ Time2 t3 = new Time2 ();
120+
121+ t3 .sethr (x2 );
122+ t3 .setmin (y2 );
123+ t3 .setsec (z2 );
124+ t3 .display ();
125+
126+ t2 .difer (t3 );
127+
128+ sc .close ();
129+
130+ }
131+ }
0 commit comments