LocalDateTime转化成字符串(显示xx分钟/小时/月前) 工具类

public class DateCal {

    /**
     * 一分钟之前的,显示刚刚
     * 几分钟前的,显示N分钟前;
     *
     * 大于1小时小于1天的,显示N小时前;
     *
     * 大于1天的,显示N天前;
     *
     * 大于1个月的,显示N个月前;
     *
     * 大于1年的,显示具体时间 年-月-日  时:分
     * @param time
     * @return
     */
    public static String getDateCompareNow(LocalDateTime time){
        String result = "";
        if (time == null){
            return result;
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        int year = now.getYear() - time.getYear();
        int month = now.getMonthValue() - time.getMonthValue();
        int day = now.getDayOfMonth() - time.getDayOfMonth();
        int hour = now.getHour() - time.getHour();
        int minute = now.getMinute() - time.getMinute();
        if (year>0){
             result = time.format(formatter);
        }else if (month>0){
            result += month+"月前";
        }else if (day>0){
            result += day+"天前";
        }else if (hour>0){
            result += hour+"小时前";
        }else if (minute>0){
            result += minute+"分钟前";
        }else {
            result += "刚刚";
        }
        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值