java中时间差计算

public class Utill {
    public String TimeString(Date currentTime, Date beginTime){
        /*默认为毫秒,除以1000是为了转换成秒*/
        long interval=(currentTime.getTime()-beginTime.getTime())/1000;//
        if(interval<0){
            interval=-interval;
        }
        long temp=0;
        String result;
        if(interval<60){
            return result="just";
        }else if((temp = interval/60)<60){
            return result=temp+(temp==1?" minute ago":" minutes ago");
        }else if((temp = temp/60) <24){
            return result=temp+(temp==1?" hour ago":" hours ago");
        }else if((temp = temp/24) <30){
            return result=temp+(temp==1?" day ago":" days ago");
        }else if((temp = temp/30) <12){
            return result=temp+(temp==1?" month ago":" months ago");
        }else{
            temp = temp/12;
            result=temp+(temp==1?" year ago":" years ago");
        }
        return result;
    }
/*if(interval<60){
            return result="刚刚";
        }else if((temp = interval/60)<60){
            return result=temp+"分钟前";
        }else if((temp = temp/60) <24){
            result = temp+"小时前";
        }else if((temp = temp/24) <30){
            result = temp+"天前";
        }else if((temp = temp/30) <12){
            result = temp+"月前";
        }else{
            temp = temp/12;
            result = temp+"年前";
        }*/

 

posted on 2016-10-21 14:18  qinyahui  阅读(587)  评论(0编辑  收藏  举报