java计算时间差值
记录下方便自己以后使用
public String computeTime(Date startTime,Date endTime){
if (StringUtils.isNotNull(startTime)){
Date gpsDate =startTime;
Date sysDate =endTime;
long gpsDateKs = gpsDate.getTime();
long sysDateJs = sysDate.getTime();
long sjc = sysDateJs - gpsDateKs;
int day = (int) (sjc / (1000 * 60 * 60 * 24));
int hour = (int) ((sjc % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
int minute = (int) (((sjc % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) / (1000 * 60));
int second = (int) ((((sjc % (1000 * 60 * 60 * 24)) % (1000 * 60 * 60)) % (1000 * 60)) / 1000);
if(day>0){
return day+"天";
}else if (hour>0){
return hour+"小时"+minute+"分钟";
}else if (minute>0){
return minute+"分钟";
}else if (second>0){
return minute + "秒";
}else {
return null;
}
}else {
return null;
}
}
|