毫秒转中文耗时格式

在开发中经常遇到毫秒数转通俗易懂的中文耗时间如1时31分26秒 现在给大家带来一段代码! 如果能用的小伙伴麻烦点个赞 直接上代码。

/**
     * 毫秒转中文耗时 (如: 1时31分26秒)
     * @author: Lu Yang
     * @date: 2019/5/15 23:54
     * @param ms
     * @return java.lang.String
     */
    public static String msToChineseTime (Long ms) {
        if (null == ms) {
            return null;
        }

        StringBuilder builder = new StringBuilder(128);

        int tmp = (int) (ms / (24 * 3600 * 1000));
        if (tmp > 0) {
            builder.append(tmp).append('天');
        }
        ms %= (24 * 3600 * 1000);
        if (ms == 0) {
            return builder.length() == 0 ? "0秒" : builder.toString();
        }

        tmp = (int) (ms / (3600 * 1000));
        if (tmp > 0) {
            builder.append(tmp).append('时');
        }
        ms %= (3600 * 1000);
        if (ms == 0) {
            return builder.length() == 0 ? "0秒" : builder.toString();
        }

        tmp = (int) (ms / (60 * 1000));
        if (tmp > 0) {
            builder.append(tmp).append('分');
        }
        ms %= (60 * 1000);
        if (ms == 0) {
            return builder.length() == 0 ? "0秒" : builder.toString();
        }

        builder.append(Math.round(ms / 1000f)).append('秒');
        return builder.toString();
    }

你可能感兴趣的:(毫秒转中文耗时格式)