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