[java]将秒数转化为“天时分秒”的格式(转贴+修改)

public class Time {
    // format seconds to day hour minute seconds style
    // Exmplae 5000s will be formatted to 1h23m20s
    public static String toDhmsStyle(long allSeconds) {
        String DateTimes = null;
        
        long days = allSeconds / (60 * 60 * 24);
        long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
        long minutes = (allSeconds % (60 * 60)) / 60;
        long seconds = allSeconds % 60;
        
        if (days > 0) {
            DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
        } else if (hours > 0) {
            DateTimes = hours + "h" + minutes + "m" + seconds + "s";
        } else if (minutes > 0) {
            DateTimes = minutes + "m" + seconds + "s";
        } else {
            DateTimes = seconds + "s";
        }

        return DateTimes;
    }

    public static void main(String[] args) {
        long mss = 5000;
        String ss = Time.toDhmsStyle(mss);
        System.out.println(ss);
    }
}

输出:

1h23m20s

参考资料:

https://blog.csdn.net/weixin_36795952/article/details/78545982

https://blog.csdn.net/qq_22019789/article/details/74665791

--END-- 2019年10月5日15:25:39

转载于:https://www.cnblogs.com/xiandedanteng/p/11624813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值