0

我需要检查密码是否过期。如果他在过去 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。

4

2 回答 2

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
于 2013-10-30T16:53:06.217 回答
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过期日期。如果您不将系统日期更改为2012,则将打印最后一条语句true。所以,我认为你做对了,你只需要检查:

if(today.after(expireDate)){
// Change Password
} else {
// proceed
}
于 2013-10-30T17:09:55.090 回答