对比时间返回时间差

//比对时间。
 public static String CompareTime(String stime){
 
 String returntime = null;
 
     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     Date curDate = new Date(System.currentTimeMillis());//获取当前时间
     String str = format.format(curDate);
   
    Date d1 = null;
    Date d2 = null;
 
    try {
        d1 = format.parse(dateStart);
        d2 = format.parse(str);
 
        //毫秒ms
        long diff = d2.getTime() - d1.getTime();
 
        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
 
        if(diffDays>0){//天
         returntime = String.valueOf(diffDays)+"天前";
        }else if(diffHours>0){//小时
         returntime = String.valueOf(diffHours)+"小时前";
        }else if(diffMinutes>0){//分钟
         returntime = String.valueOf(diffMinutes)+"分钟前";
        }else if(diffSeconds>0){
         returntime = String.valueOf(diffSeconds)+"秒前";
        }
 
    } catch (Exception e) {
        e.printStackTrace();
    }
    return returntime;
 
 }
原文地址:https://www.cnblogs.com/pastor/p/5156787.html