yangys
2024-12-30 7cd413d0f1e3235223848d47e428aad97f9f13fc
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
46
47
package com.qianwen.smartman.common.utils;
 
import java.time.Duration;
 
public class DurationUtil {
    /**
     * 将时间差转换为中文,如1天2小时3分15秒
     * @param duration
     * @return 中文时间差
     */
    public static String toChineseDuration(Duration duration) {
        String dayStr = "";
        if(duration.toDays() > 0) {
            dayStr = duration.toDays()+"天";
        }
        
        long temp;
        
        String secStr = "";
        if(duration.getSeconds() > 0) {
            temp = duration.getSeconds()%60;
            if(temp > 0) {
                secStr = temp+"秒";
            }
        }
        
        String minStr = "";
        if(duration.toMinutes() > 0) {
            temp = duration.toMinutes()%60;
            if(temp > 0) {
                minStr += temp+"分";
            }
        }
        
        String hourStr = "";
        if(duration.toHours() > 0) {
            temp = duration.toHours()%24;
            if(temp > 0) {
                hourStr = temp+"小时";
            }
        }
        
        
        
        return dayStr + hourStr+minStr+secStr;
    }
}