我需要检查密码是否过期。如果他在过去 30 天内没有修改密码,我需要要求他重置密码。这是我的代码。
Date lastPasswordModifiedDate =new SimpleDateFormat("MM/dd/yyyy").parse("10/30/2013");
if (lastPasswordModifiedDate == null)
{
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(0);
lastPasswordModifiedDate = cal.getTime();
}
Calendar lastPasswordChangeCal = GregorianCalendar.getInstance();
lastPasswordChangeCal.setTime(lastPasswordModifiedDate);
Date today = new Date();
lastPasswordChangeCal.add(Calendar.DAY_OF_MONTH, -30); //max 30 dates to expire
Date expireDate = lastPasswordChangeCal.getTime();
System.out.println(expireDate); //last password changed date
System.out.println(today); //today date - I changed in my system
System.out.println(today.after(expireDate));
当我打印这个
System.out.println(expireDate);
System.out.println(today);
System.out.println(today.after(expireDate));
Mon Sep 30 00:00:00 IST 2013
Tue Oct 30 22:07:44 IST 2012
false
我期望如果 lastPasswordModifiedDate >30 天或为 null,它应该返回 true。
最佳答案
修改后的代码:
Calendar cal = Calendar.getInstance();
Date today = cal.getTime();
if(lastPasswordModifiedDate == null)
// put today in database as lastModifiedDate and return
Date lastPasswordModifiedDate =new SimpleDateFormat("MM/dd/yyyy").parse("10/30/2013");
lastPasswordModifiedDate.add(Calendar.DAY_OF_MONTH, 30); //max 30 dates to expire
if(today.after(lastPasswordModifiedDate))
// password expired
else
// password valid
关于java - 验证上次修改日期以使用户的密码过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19688922/