我做了一个独立的方法,在密码过期之日起5个正常天之前发送电子邮件给密码将在30天后到期的相应用户。现在我很困惑如何在密码过期后的5个工作日内发送电子邮件。以下是我在5个正常日之前向用户发送通知的逻辑。
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "select EMAIL_ID from user_master where date_add(password_change_date, interval 25 day) < CURDATE();";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
String emailId = rs.getString("email_id");
System.out.println("Email Id: "+emailId);
PasswordExpiry.sendHTMLEmail( from, emailId, host, user, password, subject, body );
}
rs.close();
stmt.close();
conn.close();
PasswordExpiry.sendHTMLEmail()方法,用于向各个用户发送邮件。
答案 0 :(得分:0)
您可以使用这种通用方法查找两天之间的工作日:
public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
int workDays = 0;
//Return 0 if start and end are the same
if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
return 0;
}
if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
startCal.setTime(endDate);
endCal.setTime(startDate);
}
do {
//excluding start date
startCal.add(Calendar.DAY_OF_MONTH, 1);
if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
++workDays;
}
} while (startCal.getTimeInMillis() < endCal.getTimeInMillis()); //excluding end date
return workDays;
}
根据您的要求,此方法将返回您要过期的密码的剩余天数。如果这> 5你可以发送电子邮件。或告诉密码有效,直到像Windows一样的电子邮件天数
答案 1 :(得分:0)
考虑这个例子,我摆弄了here:
select expiry, dayname(expiry) as expiry_weekday, user,
CASE weekday(expiry)
WHEN 0 THEN date_add(expiry,INTERVAL -7 DAY) -- MON -> MON
WHEN 1 THEN date_add(expiry,INTERVAL -7 DAY) -- TUE -> TUE
WHEN 2 THEN date_add(expiry,INTERVAL -7 DAY) -- WED -> WED
WHEN 3 THEN date_add(expiry,INTERVAL -7 DAY) -- THU -> THU
WHEN 4 THEN date_add(expiry,INTERVAL -7 DAY) -- FRI -> FRI
WHEN 5 THEN date_add(expiry,INTERVAL -8 DAY) -- SAT -> FRI
WHEN 6 THEN date_add(expiry,INTERVAL -9 DAY) -- SUN -> FRI
END
as notification_date,
dayname(CASE weekday(expiry)
WHEN 0 THEN date_add(expiry,INTERVAL -7 DAY) -- MON -> MON
WHEN 1 THEN date_add(expiry,INTERVAL -7 DAY) -- TUE -> TUE
WHEN 2 THEN date_add(expiry,INTERVAL -7 DAY) -- WED -> WED
WHEN 3 THEN date_add(expiry,INTERVAL -7 DAY) -- THU -> THU
WHEN 4 THEN date_add(expiry,INTERVAL -7 DAY) -- FRI -> FRI
WHEN 5 THEN date_add(expiry,INTERVAL -8 DAY) -- SAT -> FRI
WHEN 6 THEN date_add(expiry,INTERVAL -9 DAY) -- SUN -> FRI
END
) as notification_weekday
FROM pwd_expiry ;
这会计算密码到期的星期几和基于此的通知的星期几。我不确定我的时间是否正确 - 但你应该可以在那里工作。基本上,它会根据到期日的星期几为通知创建不同的日偏移量。一旦你将WHE WHEN拉入你的WHERE状态,你就应该好好去。不仅要返回用户名,还要返回实际的到期日期,最好包含此通知电子邮件。
随着小提琴的死亡 - 这将是输入:
+-------+---------------------+
| user | expiry |
+-------+---------------------+
| jan | 2015-12-25 00:00:00 |
| xplod | 2015-12-31 00:00:00 |
+-------+---------------------+
将输出:
+---------------------+----------------+-------+---------------------+----------------------+
| expiry | expiry_weekday | user | notification_date | notification_weekday |
+---------------------+----------------+-------+---------------------+----------------------+
| 2015-12-25 00:00:00 | Friday | jan | 2015-12-18 00:00:00 | Friday |
| 2015-12-31 00:00:00 | Thursday | xplod | 2015-12-24 00:00:00 | Thursday |
+---------------------+----------------+-------+---------------------+----------------------+