评论显示时候,将时间转为类似微信空间显示的评论时间格式

1.在评论中,根据添加时间转换为类似于 刚刚、几分钱、几天前、几个月前、几年前

直接上方法:

  /**
     * 将时间转换成显示的时间格式,参考微信
     * @param addTime
     * @return
     */
    public static String getShowTime(Date addTime) {
        //当前日历时间
        Calendar currentCalendar = Calendar.getInstance();
        //发布日历时间
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(addTime);
        //当前时间单位
        int cYear = currentCalendar.get(Calendar.YEAR);
        int cMonth = currentCalendar.get(Calendar.MONTH);
        int cDate = currentCalendar.get(Calendar.DAY_OF_MONTH);
        int cHour = currentCalendar.get(Calendar.HOUR_OF_DAY);
        int cMinuter = currentCalendar.get(Calendar.MINUTE);
        //发布时间单位
        int sYear = startCalendar.get(Calendar.YEAR);
        int sMonth = startCalendar.get(Calendar.MONTH);
        int sDate = startCalendar.get(Calendar.DAY_OF_MONTH);
        int sHour = startCalendar.get(Calendar.HOUR_OF_DAY);
        int sMinuter = startCalendar.get(Calendar.MINUTE);
        if(cYear >sYear){
            return (cYear - sYear) + "年前";
        }
        else if(cMonth > sMonth){
            return (cMonth - sMonth) + "个月前";
        }
        else if(cDate > sDate){
            return (cDate - sDate) > 1 ?  (cDate - sDate) + "天前" : "昨天";
        }
        else if(cHour > sHour){
            return (cHour - sHour) + "小时前";
        }
        else if(cMinuter >= sMinuter){
            return (cMinuter - sMinuter) < 1 ? "刚刚" : (cMinuter - sMinuter) + "分钟前";
        }
        return "";
    }

 

posted @ 2019-06-10 18:35  坠落凡尘的魔鬼  阅读(344)  评论(0编辑  收藏  举报