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();
|
|
}
|
|
}
|