/**
* 输入一个日期 返回与当前时间做对比的结果 格式如下
* 小于1分钟:刚刚来过
* 小于1小时:xxx分钟前来过
* 小于1天:xxx小时前来过
* 小于31天:xxx天前来过
* 小于1年:xxx月前来过
* 大于1年:xxx年前来过
* @param d2 需要与当前时间做比较的时间
* @return
*/
public static String getTimeToStr(Date d2) {
DateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
try {
Date d1 = new Date();
//Date d2 = df.parse(“2019-02-12 11:40:24”);
long diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
if(days>365){
int years= (int) (days/365);
return years+"年前来过";
}
if(31<days&&days<=365){
int mont= (int) (days/31);
return mont+"月前来过";
}
if(0<days&&days<31){
return days+"天前来过";
}
if(0<hours&&hours<24){
return hours+"小时前来过";
}
if(0<minutes&&minutes<60){
return minutes+"分钟前来过";
}
return "刚刚来过";
}catch (Exception ex){
ex.printStackTrace();
return "";
}
}
public static String getTimeToStr2(Date d2) {
DateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
try {
Date d1 = new Date();
//Date d2 = df.parse(“2019-02-12 11:40:24”);
long diff = d1.getTime() - d2.getTime();//这样得到的差值是毫秒级别
long days = diff / (1000 * 60 * 60 * 24);
long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);
if(days>365){
int years= (int) (days/365);
return years+"年前更新";
}
if(31<days&&days<=365){
int mont= (int) (days/31);
return mont+"月前更新";
}
if(0<days&&days<31){
return days+"天前更新";
}
if(0<hours&&hours<24){
return hours+"小时前更新";
}
if(0<minutes&&minutes<60){
return minutes+"分钟前更新";
}
return "刚刚更新";
}catch (Exception ex){
ex.printStackTrace();
return "";
}
}