-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
51 lines (48 loc) · 1.27 KB
/
Time.java
File metadata and controls
51 lines (48 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.util.*;
public class Time {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.println("Enter the time in, hh mm ss format:");
int hh = inp.nextInt();
int mm = inp.nextInt();
int ss = inp.nextInt();
if(hh<=0 || mm<=0 || ss<=0){
System.out.println("Invalid Input");
System.exit(1);
}
Clock c = new Clock(hh, mm, ss);
c.display();
inp.close();
}
}
class Clock {
private int hour, min, sec;
Clock(int hh, int mm, int ss) {
hour = hh;
min = mm;
sec = ss;
check();
if (hour >=24 || hour < 0){
System.out.println("Time is invalid");
System.exit(1);
}
}
void display() {
if (hour <= 12) {
System.out.println(hour + ":" + min + ":" + sec + " AM");
} else {
int hour1 = hour - 12;
System.out.println(hour1 + ":" + min + ":" + sec + " PM");
}
}
void check(){
if(min>60){
hour = hour + 1;
min = min -60;
}
if(sec>60){
min = min + 1;
sec = sec - 60;
}
}
}