java 检查时间是否过期


    public  void clear(){

        String time = "2020-07-07 17:49:37";

        if (checktime(time,getTime())){
            System.out.println("过期=== ");

        }else {
            System.out.println("未过期===");
        }
    }

--------------------------------------------------------------------------------

    public  boolean  checktime(String end,String now){
        logger.info("###===========conf-end:"+end+",nowTime:"+now);

        JSONObject json = getDistanceTime(end, now);
        long sss = Long.valueOf(json.get("sss").toString());
        System.out.println(sss);

        if (sss>0){
            return true;
        }
        return false;
    }

    public String getTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        String nowDate =format.format(d);//获取当前时间
        //System.out.println("获取当前时间= "+ nowDate);
        return nowDate;
    }


    public JSONObject getDistanceTime(String firstLoginTime, String nowTime) {
        JSONObject dataMap = new JSONObject();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1;

        try {
            d1 = df.parse(nowTime);
            Date d2 = df.parse(firstLoginTime);// 用户初次登录时间
            long diff = d1.getTime() - d2.getTime(); 当前的时间减去我初次登陆的时间如果大于等于2小时

            dataMap.put("sss", diff);

            /*
            long seconds = diff / (1000); // 共计秒数
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            dataMap.put("hours", hours);
            dataMap.put("minutes", minutes);
            dataMap.put("seconds", seconds);
            dataMap.put("days", days);
            */
            return dataMap;
        }catch (ParseException e){

        }
        return dataMap;

    }
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我先来一碗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值