java 判断时间30分钟内有效


今天写接口的时候用到了判断时间在某一个范围内有效

验证方法:

    /**
     * 判断时间是否在时间段内
     * @param currentTime 创建时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     */
    public static boolean  decideDateRange(String currentTime,String startTime,String endTime) {
        boolean flg = false;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long currentTimeMillisecond = dateFormat.parse(currentTime).getTime();
            long startTimeMillisecond = dateFormat.parse(startTime).getTime();
            long endTimeMillisecond = dateFormat.parse(endTime).getTime();
            if(currentTimeMillisecond>=startTimeMillisecond && currentTimeMillisecond<=endTimeMillisecond){
                flg = true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return flg;
    }

调用方法(用到hutool工具类,自己去hutool官网下载):

    private String verify() {

        //验证时间戳是否在30分钟内
        long timestamp = System.currentTimeMillis();
        System.out.println(timestamp);
        Date nowDate = DateUtil.date(timestamp);
        Date paramDate = DateUtil.date(Long.valueOf(timestampParam));
        DateTime endDate = DateUtil.offsetMinute(paramDate, 30);
        boolean dateRange = decideDateRange(DateUtil.formatDateTime(nowDate) , 
     DateUtil.formatDateTime(paramDate), DateUtil.formatDateTime(endDate));
if (!dateRange) { return "时间戳已过期"; } return "true"; }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM