将秒数转为*天*小时*分*秒的形式

 秒转为“*天*小时*分*秒”:

/**
     * 将秒数转为*天*小时*分*秒的形式
     * @param time 参数:秒
     * @return
     */
    public static String formatDateTime(long time) {
        String dateTimes;
        long days = time / ( 60 * 60 * 24);
        long hours = (time % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (time % ( 60 * 60)) /60;
        long seconds = time % 60;
        if(days>0){
            dateTimes= days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
        }else if(hours>0){
            dateTimes=hours + "小时" + minutes + "分" + seconds + "秒";
        }else if(minutes>0){
            dateTimes=minutes + "分" + seconds + "秒";
        }else{
            dateTimes=seconds + "秒";
        }
        return dateTimes;
    }

 毫秒转为“*天*小时*分*秒”:

public static String formatDateTime(long ms) {
        String dateTimes;
        long ss = ms/1000;
        long days = ss / ( 60 * 60 * 24);
        long hours = (ss % ( 60 * 60 * 24)) / (60 * 60);
        long minutes = (ss % ( 60 * 60)) /60;
        long seconds = ss % 60;
        if(days>0){
            dateTimes= days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
        }else if(hours>0){
            dateTimes=hours + "小时" + minutes + "分" + seconds + "秒";
        }else if(minutes>0){
            dateTimes=minutes + "分" + seconds + "秒";
        }else{
            dateTimes=seconds + "秒";
        }
        return dateTimes;
    }

猜你喜欢

转载自blog.csdn.net/qq_24739457/article/details/82787689