验证获取的验证码是否有效的常见写法

it2025-10-05  5

验证码是否有效的常见一般都是通过时间来判断的

public static void verifyCode(){ String expireTime = "2020-10-22 10:48:50"; SimpleDateFormat sdf = new SimpleDateFormat(); Date codeDate = new Date(); try{ codeDate = sdf.parse(expireTime); long current = System.currentTimeMills(); Date curDate = new Date(current ); }catch(Exception e){ e.printStackTrace(); } if(codeDate.before(newDate)){ System.out.println("验证码已经失效"); } }

1.获取expirTime验证码的过期时间

2.通过SimpleDateFormat的parse方法转换成Date可以识别的字符串

3.判断当前的时间(newDate)是否大于expireDate,大于expireTime则失效

最新回复(0)