我正在使用 MVC 模式在 Java EE 中编写一个简单的登录程序,我希望提醒用户他/她的密码已超过一年。目前我可以从数据库中获取数据,并将其转换为字符串,但之后我不知道如何将其与当前日期进行比较。
这是我到目前为止所拥有的:
public String validateAccount(String email, String enterPassword) {
try {
Class.forName(driverName).newInstance();
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection connection;
try {
connection = DriverManager.getConnection(dbURL, username, password);
// Retrive current user data from database
String getCredentials = "SELECT id,dateSet,strength FROM users WHERE email=? AND hashPassword=SHA2(CONCAT(?, salt), 256)";
PreparedStatement verifyCredentials = connection
.prepareStatement(getCredentials);
verifyCredentials.setString(1, email);
verifyCredentials.setString(2, enterPassword);
ResultSet foundData = verifyCredentials.executeQuery();
while (foundData.next()) {
System.out.println("Found account");
int id = foundData.getInt("id");
String dateSet = foundData.getString("dateSet");
String strength = foundData.getString("strength");
// ... do something with these variables ... if
if (strength.equals("Weak")) {
return "1";
} else if (/*Check if the date in the database is over a year old, and return 2*/) {
return "2";
} else {
return "0";
}
}
foundData.close();
return "Account not found, re-enter your info again";
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}