/**
* 计算时间差,如果endTime为空则默认当前时间
* @param startTime
* @param endTime
* @param level
* @return
*/
public static float dateDiff(Date startTime, Date endTime,String level) {
if(startTime==null){
log.info("dateDiff startTime不能为空");
}
if(StringUtils.isBlank(level)){
level="h";
}
if(endTime==null){
endTime=new Date();
}
// 获得两个时间的毫秒时间差异
float diff = new Long(endTime.getTime() - startTime.getTime()).floatValue();
float result=0;
if (level.equalsIgnoreCase("d")) {
result = diff / (1000 * 24 * 60 * 60);// 计算差多少天
log.info("dateDiff day:"+result+",start:"+startTime.toString()+" end:"+endTime.toString());
return result;
} else if (level.equalsIgnoreCase("h")){
result = diff / (1000 * 60 * 60);// 计算差多少小时
log.info("dateDiff hour:"+result+",start:"+startTime.toString()+" end:"+endTime.toString());
return result;
}else if (level.equalsIgnoreCase("m")){
result = diff / (1000 * 60);// 计算差多少分钟
log.info("dateDiff min:"+result+",start:"+startTime.toString()+" end:"+endTime.toString());
return result;
}else if (level.equalsIgnoreCase("s")){
result = diff / 1000;// 计算差多少秒
log.info("dateDiff sec:"+result+",start:"+startTime.toString()+" end:"+endTime.toString());
return result;
}else{
return result;
}
}
计算时间差函数
最新推荐文章于 2023-10-25 11:40:42 发布