Java返回数据上传过了多长时间

    //返回上传时间
    static String timeString(Date createTime){
//    static String timeString(){

        //将传入的时间参数转成时间戳(秒)
        long timeLong = createTime.getTime() / 1000;
//        long timeLong = 1601088155;

        //查看传入的时间戳与当前时间的差值(秒)
        long timeDifference = ((new Date()).getTime() / 1000) - timeLong;

        //一小时之内的返回值
        if (timeDifference >= 0 && timeDifference < 60 * 60){
            Long returnTime = timeDifference / 60;
            if (returnTime == 0){
                return "刚刚上传";
            }
            return returnTime + "分钟前上传";
        }

        //一小时之后到一天之间
        if (timeDifference >= 60 * 60 && timeDifference < 60 * 60 * 24 ){
            long returnTime = timeDifference / (60 * 60);
            return returnTime + "小时前上传";
        }

        //一天到一周之间
        if (timeDifference >= 60 * 60 * 24 && timeDifference < 60 * 60 * 24 * 7){
            long returnTime = timeDifference / (60 * 60 * 24);
            return returnTime + "天前上传";
        }

        //一周到一个月之间
        if (timeDifference >= 60 * 60 * 24 * 7 && timeDifference < 60 * 60 * 24 * 30){
            long returnTime = timeDifference / (60 * 60 * 24 * 7);
            return returnTime + "周前上传";
        }

        //一个月到一年之间
        if (timeDifference >= 60 * 60 * 24 * 30 && timeDifference < 60 * 60 * 24 * 365){
            long returnTime = timeDifference / (60 * 60 * 24 * 30);
            //规范写的是一个模糊值 12进行判断为11月前
            if (returnTime == 12){
                return "11月前上传";
            }
            return returnTime + "月前上传";
        }

        //一年即一年之后
        if (timeDifference >= 60 * 60 * 24 * 365){
            long returnTime = timeDifference / (60 * 60 * 24 * 365);
            return returnTime + "年前上传";
        }

        return "";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值