我需要检查密码是否已过期。如果他没有修改过去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。
答案 0 :(得分:1)
修改后的代码:
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
答案 1 :(得分:1)
由于您已将系统时间更改为Tue Oct 30 22:07:44 IST 2012
,因此这些语句
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));
打印这些输出
Mon Sep 30 00:00:00 IST 2013
Tue Oct 30 22:07:44 IST 2012
false
因为today
不是after
expireDate。如果您不将系统日期更改为2012
,则最后一个语句将打印true
。所以,我认为你正确地做到了,你只需要检查:
if(today.after(expireDate)){
// Change Password
} else {
// proceed
}