-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime2.java
80 lines (80 loc) · 2.22 KB
/
time2.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
public class time2{
private int hour;
private int minute;
private int second;
public time2(){
this(0,0,0);
}
public time2(int hour){
this(hour,0,0);
}
public time2(int hour,int minute){
this(hour,minute,0);
}
public time2(int hour,int minute,int second) {
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.hour = hour;
this.minute = minute;
this.second = second;
}
/*public time2(time2 time){
this(time.gethour,time.getminute,time.getsecond);
}*/
public void setTime(int hour, int minute, int second){
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
if (minute < 0 || minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
if (second < 0 || second >= 60)
throw new IllegalArgumentException("second must be 0-59");
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void setHour(int hour){
if (hour < 0 || hour >= 24)
throw new IllegalArgumentException("hour must be 0-23");
this.hour = hour;
}
public void setMinute(int minute){
if (minute < 0 && minute >= 60)
throw new IllegalArgumentException("minute must be 0-59");
this.minute = minute;
}
public void setSecond(int second){
if (second >= 0 && second < 60)
throw new IllegalArgumentException("second must be 0-59");
this.second = second;
}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
public int getSecond(){
return second;
}
public String toUniversalString(){
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
public String toString() {
return String.format("%d:%02d:%02d %s", ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
}
public void tick(){
second=second+1;
}
public int increminute(){
minute+=1;
return minute;
}
public int increhour(){
hour+=1;
return hour;
}
}