gpt4 book ai didi

java - 验证上次修改日期以使用户的密码过期

转载 作者:行者123 更新时间:2023-12-02 06:35:34 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com