-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTime.java
More file actions
53 lines (45 loc) · 1.4 KB
/
Copy pathMyTime.java
File metadata and controls
53 lines (45 loc) · 1.4 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
52
53
public class MyTime {
int hour, minute;
public MyTime(int hour, int minute) {
this.hour = hour;
this.minute = minute;
}
@Override
public String toString() {
return (hour < 10 ? "0":"") + hour + ":" + (minute < 10 ? "0":"")+minute;
}
public void incrementHour() {
incrementHour(1);
}
public int incrementHour(int diff) {
int dayDiff = 0;
if (hour + diff < 0)
dayDiff = -1;
dayDiff += (hour + diff) / 24;
hour = (hour + diff) % 24;
if (hour < 0)
hour += 24;
return dayDiff;
}
public int incrementMinute(int diff) {
int hourDiff = 0;
if (minute + diff < 0)
hourDiff = -1;
hourDiff += (minute + diff) / 60;
minute = (minute + diff) % 60;
if (minute < 0)
minute += 60;
return incrementHour(hourDiff);
}
public int totalMinutesDifference(MyTime anotherTime) {
int thisTotalMinutes = hour * 60 + minute;
int anotherTotalMinutes = anotherTime.hour * 60 + anotherTime.minute;
return Math.abs(thisTotalMinutes - anotherTotalMinutes);
}
public boolean isAfter(MyTime time) {
return (hour * 60) + minute > (time.hour * 60) + time.minute;
}
public boolean isBefore(MyTime time) {
return (hour * 60) + minute < (time.hour * 60) + time.minute;
}
}