-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeIntervalHelper.java
More file actions
161 lines (142 loc) · 5.32 KB
/
Copy pathTimeIntervalHelper.java
File metadata and controls
161 lines (142 loc) · 5.32 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
package gew.caching.util;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* Time Interval Helper, Conversion Between Days, Hours, Minutes, Seconds and Milliseconds.
* Provide User Friendly Method to Input Time Unit as Below:
* (days/day/d, hours/hour/h, minutes/minute/min/m, seconds/second/sec/s)
* This helper class will be under continuous development.
*
* @author Jason/GeW
* @since 2018-09-14
*/
public class TimeIntervalHelper {
private static final String DIGIT_CHARACTER_SPLIT = "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)";
private TimeIntervalHelper() {
}
public static Long convertDaysToMilliSeconds(long days) {
Duration duration = Duration.ofDays(days);
return duration.getSeconds() * 1000;
}
public static Long convertHoursToMilliSeconds(long hours) {
Duration duration = Duration.ofHours(hours);
return duration.getSeconds() * 1000;
}
public static Long convertMinutesToMilliSeconds(long minutes) {
Duration duration = Duration.ofMinutes(minutes);
return duration.getSeconds() * 1000;
}
public static long convertSecondsToMilliSeconds(long seconds) {
return seconds * 1000;
}
public static long convertMilliSecondsToSeconds(long milliseconds) {
return milliseconds / 1000;
}
public static long convertTimeToMilliSeconds(int hours, int minutes, int seconds) {
return convertHoursToMilliSeconds(hours)
+ convertMinutesToMilliSeconds(minutes)
+ convertSecondsToMilliSeconds(seconds);
}
public static long convertTimeToMilliSeconds(int days, int hours, int minutes, int seconds) {
return convertDaysToMilliSeconds(days)
+ convertHoursToMilliSeconds(hours)
+ convertMinutesToMilliSeconds(minutes)
+ convertSecondsToMilliSeconds(seconds);
}
public static Long convertTimeToMilliSeconds(long time, final String unit) {
if (unit == null || unit.isEmpty()) {
throw new IllegalArgumentException("Invalid Time Unit");
} else {
switch (unit.toLowerCase().trim()) {
case "days":
case "day":
case "d":
return convertDaysToMilliSeconds(time);
case "hours":
case "hour":
case "h":
return convertHoursToMilliSeconds(time);
case "minutes":
case "minute":
case "min":
case "m":
return convertMinutesToMilliSeconds(time);
case "seconds":
case "second":
case "sec":
case "s":
return convertSecondsToMilliSeconds(time);
default:
throw new IllegalArgumentException("Unidentified Time Unit: " + unit);
}
}
}
public static Long convertTimeToMilliSeconds(long time, final TimeUnit unit) {
Long result;
if (unit == null) {
throw new IllegalArgumentException("Invalid Time Unit");
}
switch (unit) {
case DAYS:
result = convertDaysToMilliSeconds(time);
break;
case HOURS:
result = convertHoursToMilliSeconds(time);
break;
case MINUTES:
result = convertMinutesToMilliSeconds(time);
break;
case SECONDS:
result = convertSecondsToMilliSeconds(time);
break;
default:
throw new IllegalArgumentException("Unsupported Time Unit");
}
return result;
}
public static Long convertTimeToMilliSeconds(final String timeWithUnit) {
if (timeWithUnit == null || timeWithUnit.isEmpty()) {
throw new IllegalArgumentException("Invalid TimeWithUnit Input");
}
String[] components = timeWithUnit.split(DIGIT_CHARACTER_SPLIT);
if (components.length < 2) {
throw new IllegalArgumentException("Missing Time Unit Field");
} else {
return convertTimeToMilliSeconds(Long.parseLong(components[0].trim()), components[1].trim());
}
}
public static TimeUnit convertTimeUnitFromString(final String unit) {
if (unit == null) {
throw new IllegalArgumentException("Invalid Time Unit");
}
switch (unit.toLowerCase().trim()) {
case "days":
case "day":
case "d":
return TimeUnit.DAYS;
case "hours":
case "hour":
case "h":
return TimeUnit.HOURS;
case "minutes":
case "minute":
case "min":
case "m":
return TimeUnit.MINUTES;
case "seconds":
case "second":
case "sec":
case "s":
return TimeUnit.SECONDS;
case "milliseconds":
case "millisecond":
case "ms":
return TimeUnit.MILLISECONDS;
default:
throw new IllegalArgumentException("Unidentified Time Unit: " + unit);
}
}
public static Duration convertMilliSecondsToDuration(long milliseconds) {
return Duration.ofMillis(milliseconds);
}
}