lyh
2025-06-27 f472efc9ad1f9bda645dd39b40607c5eabb73b47
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.jeecg.modules.flowable.util;
 
import java.util.Date;
 
/**
 * 描述:时间工具类
 *
 * @Date 2023/10/11
 */
public class TimeUtil {
 
    /**一天*/
    public static final long day    = 1000 * 60 * 60 * 24;
    /**一小时*/
    public static final long hour   = 1000 * 60 * 60;
    /**一分钟*/
    public static final long minute = 1000 * 60;
    /**一秒*/
    public static final long second = 1000;
 
    /**
     * 计算两个时间差,以时分秒的方式展现。
     * @param start
     * @param end
     * @param type 1保留毫秒 2保留秒 3保留分钟 4保留小时 5保留天
     * @return
     */
    public static String howLong(Date start, Date end, int type){
        if(start == null || end == null)
            return "";
        return howLong(start.getTime(), end.getTime(), type);
    }
 
    /**
     * 计算两个时间差,以时分秒的方式展现。
     * @param start
     * @param end
     * @return
     */
    public static String howLong(Date start, Date end){
        if(start == null || end == null)
            return "";
        return howLong(start.getTime(), end.getTime(), 1);
    }
 
    /**
     * 计算给定时间距离现在的时间差,以时分秒的方式展现。
     * @param startTime
     * @return
     */
    public static String howLong(long startTime){
        return howLong(startTime, System.currentTimeMillis(),1);
    }
 
    /**
     * 计算耗时时长
     * @param startTime 起点时间,毫秒数
     * @return
     */
    public static String howLong(long startTime, long endTime ,int type) {
        long millis = endTime - startTime;
        return howLongByMillis(millis, type);
    }
 
    /**
     * 计算耗时时长
     * @param millis
     * @param type
     * @return
     */
    public static String howLongByMillis(long millis, int type) {
        if(millis <= 0) return "小于1秒";
 
        if(type > 5) return "格式错误";
 
        StringBuilder sb = new StringBuilder();
 
        //天
        if(millis >= day && type <=5){
            long days = millis / day;
            sb.append(days).append("天");
            millis -= days * day;
        }
        // 小时计
        if(millis >= hour && type <=4){
            long hours = millis / hour;
            sb.append(hours).append("小时");
            millis -= hours * hour;
        }
 
        if(millis >= minute && type <=3){
            long minutes = millis / minute;
            sb.append(minutes).append("分钟");
            millis -= minutes * minute;
        }
 
        if(millis >= second && type <=2){
            long seconds = millis / second;
            sb.append(seconds).append("秒");
            millis -= seconds * second;
        }
 
        if(millis > 0 && type <=1){
            sb.append(millis).append("毫秒");
        }
 
        return sb.toString();
 
    }
 
}