java 处理几分钟前,几小时前

public static String dateDifferenceDesc(Date now, Date early) {
        String res = "";
        if (null != now && null != early) {
            Long preTime = now.getTime() - early.getTime();
            if (preTime < 60000L) {
                res = "1分钟前";
            } else if (preTime < 3600000L) {
                res = (preTime / 60000L) + "分钟前";
            } else if (preTime < 86400000L) {
                res = (preTime / 3600000L) + "小时前";
            } else if (preTime < 172800000L) {
                res = "昨天";
            } else if (preTime < 259200000L) {
                res = "前天";
            } else if (preTime < 31536000000L) {
                res = (preTime / 86400000L) + "天前";
            } else {
                res = (preTime / 31536000000L) + "年前";
            }
        }
        return res;
    }

 

posted @ 2021-03-12 10:41  二次元的程序猿  阅读(663)  评论(0编辑  收藏  举报