Home 将时间转化为天小时分秒字符串
Post
Cancel

将时间转化为天小时分秒字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public String formatSeconds(String secondsStr) {
    Long seconds = new BigDecimal(secondsStr).longValue();
    String timeStr = seconds + "秒";
    if (seconds > 60) {
        long second = seconds % 60;
        long min = seconds / 60;
        if (second == 0) {
            timeStr = min + "分钟";
        } else {
            timeStr = min + "分钟" + second + "秒";
        }
        if (min > 60) {
            min = (seconds / 60) % 60;
            long hour = (seconds / 60) / 60;
            if (second == 0) {
                if (min == 0) {
                    timeStr = hour + "小时";
                } else {
                    timeStr = hour + "小时" + min + "分钟";
                }
            } else {
                timeStr = hour + "小时" + min + "分钟" + second + "秒";
            }
            if (hour > 24) {
                hour = ((seconds / 60) / 60) % 24;
                long day = (((seconds / 60) / 60) / 24);
                timeStr = day + "天" + hour + "小时" + min + "分钟" + second + "秒";
                if (second == 0) {
                    if (min == 0) {
                        if (hour == 0) {
                            timeStr = day + "天";
                        } else {
                            timeStr = day + "天" + hour + "小时";
                        }
                    } else {
                        timeStr = day + "天" + hour + "小时" + min + "分钟";
                    }
                } else {
                    timeStr = day + "天" + hour + "小时" + min + "分钟" + second + "秒";
                }
            }
        }
    }
    return timeStr;
}
This post is licensed under CC BY 4.0 by the author.