-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeFormat.java
More file actions
43 lines (39 loc) · 1.58 KB
/
TimeFormat.java
File metadata and controls
43 lines (39 loc) · 1.58 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
/**
* Utility class containing methods for converting time for display.
*/
public final class TimeFormat {
/**
* Converts time in seconds to hours, minutes and seconds.
* @param secondsSum time to convert, in seconds. Must be non-negative and <360000.
* @return string representing time, in format HH:MM:SS.
*/
public static String SecondsToReadableTime(int secondsSum) {
if(secondsSum < 0 || secondsSum >= 360000) {
throw new IllegalArgumentException("Number of seconds must be non-negative and below 360000.");
}
return FormatTime(SecondsToHoursMinutesSeconds(secondsSum));
}
/**
* Formats a {@link Time} structure into a string, in format HH:MM:SS.
* @param time the time to format.
* @return String in format HH:MM:SS.
*/
public static String FormatTime(Time time) {
return String.format("%02d:%02d:%02d", time.hours, time.minutes, time.seconds);
}
/**
* Converts seconds into a {@link Time} structure, containing hours, minutes and seconds.
* @param secondsSum time to convert, in seconds. Must be non-negative and <360000.
* @return Time object containing hours (0-99), minutes (0-59) and seconds (0-59).
*/
public static Time SecondsToHoursMinutesSeconds(int secondsSum) {
if(secondsSum < 0 || secondsSum >= 360000) {
throw new IllegalArgumentException("Number of seconds must be non-negative and below 360000.");
}
int hours = secondsSum / 3600;
int remainderAfterHours = secondsSum - hours * 3600;
int minutes = remainderAfterHours / 60;
int seconds = remainderAfterHours - minutes * 60;
return new Time(hours, minutes, seconds);
}
}