-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
164 lines (143 loc) · 4.94 KB
/
Time.java
File metadata and controls
164 lines (143 loc) · 4.94 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Scanner;
import java.util.Scanner;
/**
* A Time object represents a twenty four hour clock reading composed of hours, minutes and seconds.
*
*
* @author Stephan Jamieson
* @version 30/5/10
*/
public class Time implements Comparable<Time> {
public final static Time MIDNIGHT = new Time(0, 0);
public final static Time MIDDAY = new Time(12,0);
private Duration time;
/**
* Create a Time object from the given duration.
*
* The given value is mapped onto the twenty four hour clock. So for example, a duration of
* 7 hours, 5 minutes produces a Time object representing the value 07:05.
*
* If the given value exceeds a twenty four hour period then it is "wrapped around".
* So for example, a duration of 25.5 hours produces a Time object representing the value 01:30.
*
* @param duration the value representing the desired time.
*/
public Time(Duration duration) {
duration = duration.remainder(Duration.ONE_DAY);
if (duration.isNegative()) {
duration = duration.add(Duration.ONE_DAY);
}
this.time = duration;
}
/**
* Create a Time object representing the given time in hours and minutes.
*/
public Time(int hours, int minutes) {
this(hours, minutes, 0);
}
/**
* Create a Time object representing the given time in hours, minutes and seconds.
*/
public Time(int hours, int minutes, int seconds) {
time = (new Duration("hour", hours)).add(new Duration("minute", minutes))
.add(new Duration("second", seconds));
}
public Time(String string) {
Scanner scanner = new Scanner(string.trim()).useDelimiter(":");
int hours = scanner.nextInt();
int minutes = scanner.nextInt();
int seconds = 0;
if (scanner.hasNextInt()) { seconds = scanner.nextInt(); }
time = new Duration("hour", hours).add(new Duration("minute", minutes))
.add(new Duration("second", seconds));
}
/**
* Obtain the hours component of the time value represented.
*/
public int getHours() {
return (int)(this.asDuration().divideBy(Duration.ONE_HOUR));
}
/**
* Obtain the minutes component of the time value represented.
*/
public int getMinutes() {
Duration result = this.asDuration();
return (int)(result.remainder(Duration.ONE_HOUR).divideBy(Duration.ONE_MINUTE));
}
/**
* Obtain the seconds component of the time value represented.
*/
public int getSeconds() {
Duration result = this.asDuration();
return (int)(result.remainder(Duration.ONE_MINUTE).divideBy(Duration.ONE_SECOND));
}
/**
* Translate this Time object into the equivalent Duration.
*/
public Duration asDuration() {
return new Duration(this.time);
}
/**
* Obtain the Time that results from adding the given period to this Time.
*/
public Time add(Duration duration) {
return new Time(this.asDuration().add(duration));
}
/**
* Obtain the Time that results from subtracting the given period from this Time.
*/
public Time subtract(Duration duration) {
return new Time(this.asDuration().subtract(duration));
}
/**
* Obtain the period between this time and the given time by subtracting the latter
* from the former.
*/
public Duration subtract(Time other) {
return subtract(other.asDuration()).asDuration();
}
/**
* Returns true if the given object is a Time object and represents the same time value as this Time
* object, otherwise returns false.
*/
public boolean equals(Object o) {
if (!(o instanceof Time)) {
return false;
}
else {
Time other = (Time)o;
return this.asDuration().equals(other.asDuration());
}
}
/**
* Obtain a hashcode value for this object.
*/
public int hashCode() {
return this.asDuration().hashCode();
}
/**
* Compare this Time object with the other Time object, returning -1, 0 or 1, depending on whether this
* Time precedes, is equal to, or exceeds the other time value.
*
*/
public int compareTo(Time other) {
return this.asDuration().compareTo(other.asDuration());
}
private String format(int value) {
if (value<10) {
return "0"+value;
}
else {
return ""+value;
}
}
/**
* Obtain a String representation of this Time.
*/
public String toString() {
return format(this.getHours())+":"+format(this.getMinutes())+":"+format(this.getSeconds());
}
public static boolean isValid(String string) {
return string.matches("\\d:[0-5]\\d")||string.matches("[01]\\d:[0-5]\\d")||string.matches("2[0-3]:[0-5]\\d");
}
}