Repository navigation
Expand file tree
/
Copy pathTimeUtil.java
More file actions
64 lines (56 loc) · 1.79 KB
/
Copy pathTimeUtil.java
File metadata and controls
64 lines (56 loc) · 1.79 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
package test;
/**
* This gets closer to a real class which might do something. These are
* static methods, used to operate on a prescribed representation of time.
* @author jbf
*/
public class TimeUtil {
public static int HOUR=0;
public static int MINUTE=1;
public static int SECOND=2;
/**
* create a time array
* @param h the hours
* @param m the minutes
* @param s the seconds
*/
public static int[] create( int h, int m, int s ) {
return new int[] { h, m, s };
}
/**
* print the time to stdout.
* @param time three-component time array
*/
public static void print( int[] time ) {
System.out.println(toString(time));
}
/**
* Add a component, hours or minutes or seconds, to the time.
*
* @param time, a three-element int array of [ hours, minutes, seconds ]
* @param component, SECOND or MINUTE or HOUR
* @param s, the number of that component
*/
public static void addComponent( int [] time, int component, int s) {
if ( component==SECOND ) {
time[SECOND]= time[SECOND] + s;
} else if ( component==MINUTE ) {
time[MINUTE]= time[MINUTE] + s;
} else if ( component==HOUR ) {
time[HOUR]= time[HOUR] + s;
}
while ( time[SECOND] > 60) {
time[MINUTE] = time[MINUTE] + 1;
time[SECOND] = time[SECOND]- 60;
}
if ( time[MINUTE] > 60) {
int hr= time[MINUTE] / 60;
int mn= time[MINUTE] - 60 * hr;
time[HOUR] = time[HOUR] + hr;
time[MINUTE] = time[MINUTE] - hr*60 + mn;
}
}
public static String toString( int[] time ) {
return String.format("%02d:%02d:%02d", time[0], time[1], time[2] );
}
}