public static void main(String[] args) {
System.out.println(getSecondDescription(864));
System.out.println(getSecondDescription(9640));
System.out.println(getSecondDescription(3600000 * 2));
System.out.println(getSecondDescription(96400000));
System.out.println(getBytesSizeDescription(1024 * 16L));
System.out.println(getBytesSizeDescription(1024L * 1024L * 16L));
System.out.println(getBytesSizeDescription(1024L * 1024L * 1024L * 16L));
System.out.println(getBytesSizeDescription(1024L * 1024L * 1024L * 1024L * 16L));
System.out.println(getBytesSizeDescription(1024L * 1024L * 1024L * 1024L * 1024L * 16L));
}
private static long half_rounded(long x) {
return (x + (x < 0 ? -1 : 1)) / 2;
}
public static String getBytesSizeDescription(long size) {
final long limit = 10 * 1024;
final long limit2 = limit * 2 - 1;
if (Math.abs(size) < limit) {
return String.format("%d bytes", size);
} else {
size /= (1 << 9);
if (Math.abs(size) < limit2) {
return String.format("%d kB", half_rounded(size));
} else {
size /= (1 << 10);
if (Math.abs(size) < limit2) {
return String.format("%d MB", half_rounded(size));
} else {
size /= (1 << 10);
if (Math.abs(size) < limit2)
return String.format("%d GB", half_rounded(size));
else {
size /= (1 << 10);
return String.format("%d TB", half_rounded(size));
}
}
}
}
}
public static String getSecondDescription(double elapsed_msec) {
double seconds;
double minutes;
double hours;
double days;
if (elapsed_msec < 1000.0)
return String.format("%.3f ms", elapsed_msec / 1000f);
seconds = elapsed_msec / 1000.0;
minutes = Math.floor(seconds / 60.0);
seconds -= 60.0 * minutes;
if (minutes < 60.0)
return String.format("%02d:%06.3f", (int) minutes, seconds);
hours = Math.floor(minutes / 60.0);
minutes -= 60.0 * hours;
if (hours < 24.0)
return String.format("%02d:%02d:%06.3f", (int) hours, (int) minutes, seconds);
days = Math.floor(hours / 24.0);
hours -= 24.0 * days;
return String.format("%.0f d %02d:%02d:%06.3f", days, (int) hours, (int) minutes, seconds);
}
输出
0.864 ms
00:09.640
02:00:00.000
1 d 02:46:40.000
16 kB
16 MB
16 GB
16 TB
16384 TB