在web开发过程中经常会遇到一些小细节的东西,下面这段是计算时间差的.
public String computeDate(Date date){
String returnStr = "";
Long nowTime = System.currentTimeMillis();
Long oldTime = date.getTime();
Long diff = (nowTime - oldTime)/(1000*60);//分钟
if(diff<0){
returnStr = "未来";
}else if(diff>=0 && diff<1){
returnStr = "刚刚";
}else if(diff>=1 && diff<60){
returnStr = diff + "分钟前";
}else if(diff>=60 && diff<1440){
diff = diff/60;
if(diff<=1){
diff = 1L;
}
returnStr = diff + "小时前";
}else if(diff>=1440 && diff<43200){
diff = diff/60/24;
if(diff<=1){
returnStr = "1天前";
}else{
returnStr = diff + "天前";
}
}else if(diff>=43200 && diff<525600){
diff = diff/60/24/30;
if(diff<=1){
returnStr = "1月前";
}else if(diff>=12){
returnStr = "1年前";
}else{
returnStr = diff + "月前";
}
}else{
diff = diff/60/24/365;
if(diff<=1){
diff = 1L;
}
returnStr = diff + "年前";
}
return returnStr;
}