嗨,这段代码怎么样?
example)
0 ms -> 0 ms
846 ms -> 846ms
1,000 ms -> 1s
1,034 ms -> 1s 34ms
60,000 ms -> 1m
94,039 ms -> 1m 34s 39ms
3,600,000 ms -> 1h
61,294,039 ms -> 17h 1m 34s 39ms
86,400,000 ms -> 1d
406,894,039 ms -> 4d 17h 1m 34s 39ms
31,536,000,000 ms -> 1y
50,428,677,591 ms -> 1y 218d 15h 57m 57s 591ms
50,428,677,591 ns -> 50s 428ms 677us 591ns
50,428,677,591 us -> 14h 28s 677ms 591us
50,428,677,591 ms -> 1y 218d 15h 57m 57s 591ms
50,428,677,591 s -> 1599y 30d 5h 59m 51s
50,428,677,591 m -> 95944y 354d 23h 51m
50,428,677,591 h -> 5756698y 129d 15h
50,428,677,591 d -> 138160760y 191d
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
public class TimeUtils {
private static final TimeUnitInfo[] TIME_UNIT_INFO = new TimeUnitInfo[] {
new TimeUnitInfo(TimeUnit.NANOSECONDS, "ns")
, new TimeUnitInfo(TimeUnit.MICROSECONDS, "us")
, new TimeUnitInfo(TimeUnit.MILLISECONDS, "ms")
, new TimeUnitInfo(TimeUnit.SECONDS, "s")
, new TimeUnitInfo(TimeUnit.MINUTES, "m")
, new TimeUnitInfo(TimeUnit.HOURS, "h")
, new TimeUnitInfo(TimeUnit.DAYS, "d")
};
private static final Map<TimeUnit, String> UNIT_STR = new HashMap<>();
static {
for (TimeUnitInfo tui : TIME_UNIT_INFO) {
UNIT_STR.put(tui.unit, tui.unitStr);
}
}
private static final Function<TimeUnit, TimeUnitInfo[]> FN_TIME_UNITS = unit -> {
ArrayList<TimeUnitInfo> units = new ArrayList<>();
for (TimeUnitInfo tui : TIME_UNIT_INFO) {
if (tui.unit.ordinal() >= unit.ordinal()) {
units.add(tui);
}
}
return units.toArray(new TimeUnitInfo[] {});
};
public static final int DC_NONE = 0x00;
public static final int DC_NANO = 0x01;
public static final int DC_MICRO = DC_NANO << 1;
public static final int DC_MILLI = DC_MICRO << 1;
public static final int DC_SECOND = DC_MILLI << 1;
public static final int DC_MINUTE = DC_SECOND << 1;
public static final int DC_HOUR = DC_MINUTE << 1;
public static final int DC_DAY = DC_HOUR << 1;
private TimeUtils() {
}
public static void main(String[] args) {
long[] times = new long[] { 0, 846, 1000, 1034, 60000, 94039, 3600000, 61294039, 86400000, 406894039, 31536000000L, 50428677591L };
for (long time : times) {
System.out.println(String.format("%20s %-2s -> %s", String.format("%,d", time), "ms", toFormattedString(time, TimeUnit.MILLISECONDS)));
}
System.out.println("=================================");
long time = 50428677591L;
for (TimeUnitInfo tui : TIME_UNIT_INFO) {
System.out.println(String.format("%20s %-2s -> %s", String.format("%,d", time), tui.unitStr, toFormattedString(time, tui.unit)));
}
}
private static long mod(long time, TimeUnit unit) {
switch (unit) {
case NANOSECONDS:
case MILLISECONDS:
case MICROSECONDS:
return time % 1000;
case SECONDS:
return time % 60;
case MINUTES:
return time % 60;
case HOURS:
return time % 24;
case DAYS:
return time % 365;
default:
throw new IllegalArgumentException(unit.toString());
}
}
private static void prependTimeAndUnit(StringBuffer timeBuf, long time, String unit) {
if (time < 1) {
return;
}
if (timeBuf.length() > 0) {
timeBuf.insert(0, " ");
}
timeBuf.insert(0, unit);
timeBuf.insert(0, time);
}
public static String toFormattedString(long time, TimeUnit timeUnit) {
if (time < 1) {
return "0 " + UNIT_STR.get(timeUnit);
}
StringBuffer timeBuf = new StringBuffer();
long mod = 0L;
long up = time;
for (TimeUnitInfo unit : FN_TIME_UNITS.apply(timeUnit)) {
mod = mod(up, unit.unit);
prependTimeAndUnit(timeBuf, mod, unit.unitStr);
up = up(up, unit.unit);
if (up < 1) {
return timeBuf.toString();
}
}
prependTimeAndUnit(timeBuf, up, "y");
return timeBuf.toString();
}
private static long up(long time, TimeUnit unit) {
switch (unit) {
case NANOSECONDS:
case MILLISECONDS:
case MICROSECONDS:
return time / 1000;
case SECONDS:
return time / 60;
case MINUTES:
return time / 60;
case HOURS:
return time / 24;
case DAYS:
return time / 365;
default:
throw new IllegalArgumentException(unit.toString());
}
}
private static class TimeUnitInfo {
private final TimeUnit unit;
private final String unitStr;
public TimeUnitInfo(TimeUnit unit, String unitStr) {
this.unit = unit;
this.unitStr = unitStr;
}
}
}