java 将字节大小和毫秒转换为人类易于理解的单位

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));
}


//region TODO:将字节大小转换为人类易于理解的单位

/**
 * 除以二,从零舍入
 *
 * @param x
 * @return
 */
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);        /* keep one extra bit for rounding */
        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));
                }
            }
        }
    }
}
//endregion

//region TODO:将毫秒转换为人类易于理解的单位
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);
}
//endregion

输出

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kmblack1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值