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;

    }
    

猜你喜欢

转载自blog.csdn.net/jack_bob/article/details/107201622