01: package dalma;
02:
03: import java.util.Date;
04:
05: /**
06: * Unit of time.
07: *
08: * @author Kohsuke Kawaguchi
09: */
10: public enum TimeUnit {
11: NANOSECONDS(1), MICROSECONDS(1000L), MILLISECONDS(1000L * 1000), SECONDS(
12: 1000L * 1000 * 1000), MINUTES(1000L * 1000 * 1000 * 60), HOURS(
13: 1000L * 1000 * 1000 * 60 * 60), DAYS(1000L * 1000 * 1000
14: * 60 * 60 * 24), WEEKS(1000L * 1000 * 1000 * 60 * 60 * 24
15: * 7), YEARS(1000L * 1000 * 1000 * 60 * 60 * 24 * 365);
16:
17: private final long unitTime;
18:
19: TimeUnit(long unitTime) {
20: this .unitTime = unitTime;
21: }
22:
23: /**
24: * Converts to nanoseconds.
25: */
26: public long toNano(long time) {
27: if (time > Long.MAX_VALUE / unitTime)
28: return Long.MAX_VALUE;
29: return time * unitTime;
30: }
31:
32: public long toMilli(long time) {
33: return toNano(time) / MILLISECONDS.unitTime;
34: }
35:
36: /**
37: * Gets the {@link Date} by ading the duration to the current time.
38: */
39: public Date fromNow(long time) {
40: return new Date(System.currentTimeMillis() + toMilli(time));
41: }
42: }
|