我正在使用MVC模式在Java EE中編寫簡單的登錄程序,並且希望提醒用戶他/她的密碼已超過一年。目前我可以從數據庫中獲取數據,並將其轉換爲字符串,但之後我不知道如何將其與當前日期進行比較。如何比較從java當前日期mySQL日期?
這是我到目前爲止有:
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 "";
}
我不與.getTimestamp()方法,或者用一般的日期familizarized的差異。 .getTimestamp返回什麼格式,以及如何轉換它? – 2013-03-07 18:50:34
假設數據庫中的列是DATE類型,那麼gettimestamp將返回一個Timestamp實例,然後可以給你一個long,這是自unixepoc以來的毫秒數。 Date類有一個構造函數,它代表了自unixepoc作爲參數以來的毫秒數,因此您可以獲得日期實例:) – JustDanyul 2013-03-07 18:52:49
您是男人!我認爲它工作。 :D – 2013-03-07 19:05:40