Comparing timestamps (in the start time and end time)

Comparing timestamps (in the start time and end time)

The idea is very simple to achieve, generally use it for logon to determine, for example, when the user logged on to check whether the range is still within the validity period, if not within the start time and end time of, then it indicates that the user has expired;

In this case, first of all you can think of three parameters, the first need to get the current system time, because we need to log in to determine the current time, whether the user is still within the validity period, and then get the user database start time and end time, these two fields need to be present in the database;

1. First of all, I need to need to write a uti class, create a method function:

    /**
     * 时间格式化转换
     * @param time
     * @return
     */
    public static long dateToTimestamp(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = simpleDateFormat.parse(time);
            long ts = date.getTime()/1000;
            return ts;
        } catch (ParseException e) {
            return 0;
        }
    }

    / ** 
     * Returns the Boolean parameter passed, logical judgment in this method, if the current time (currenttime) on the inside of the end time (endTime) at the start time (startTime) 
     * returns true, otherwise it returns false 
     * @ param currentTime 
     * @param the startTime 
     * @param endTime 
     * @return 
     * / 
    public  static  Boolean isUserChick (currentTime String, String the startTime, endTime String) {
        Long   C = DateUtil.dateToTimestamp (currentTime);
         Long S = dateToTimestamp (the startTime);
         Long E = DateUtil.dateToTimestamp (endTime);
         IF (S <C && C <e){
            return  true;
        }else {
            return  false;
        }
    }

2. Call DateUtil in the interceptor logged in isUserChick method can be, you need to pass three parameters, the current system time, start time, end time

/ ** 
             * determine whether the normal user account 
             * Log determine whether the current time in the start time and end time within the range, if not prompt the account expires 
             * / 
// Get the current system time 
                a Date DATE = new new a Date (); 
                SimpleDateFormat dateFormat = new new SimpleDateFormat ( "the MM-dd-YYYY"); // can easily modify the date format 
                String = currentTime dateFormat.format (dATE);
                 // get the user start time 
                String the startTime = user.getStartTime ();
                 // get the user end time 
                String = endTime user.getEndTime ();
                 // determines whether the account is expired 
                boolean flag=DateUtil.isUserChick(currenttime,startTime,endTime);
                if (flag==false){
                    throw new ExpiredCredentialsException();
                }

Because we landed uses Shiro interceptor, so if a user no longer find within this range, an exception is thrown directly to the front-end can be;

Additional logic

The module also need to point is that, if passed over the start time and end time is empty, then that proves that the user is permanent, no longer enter the time stamp judgment:

if (StringUtil.isNotEmpty(user.getStartTime())&&StringUtil.isNotEmpty(user.getEndTime())){


}

Guess you like

Origin www.cnblogs.com/StanleyBlogs/p/11021859.html