MVC 패턴을 사용하여 Java EE에 간단한 로그인 프로그램을 작성 중이며 비밀번호가 1 년 넘었 음을 사용자에게 알리고 싶습니다. 현재 데이터베이스에서 데이터를 가져 와서 문자열로 변환 할 수 있지만 그 이후에는 현재 날짜와 비교하는 방법을 알지 못합니다. 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가 반환되며 어떻게 변환합니까? –
데이터베이스의 열이 DATE 유형이라는 가정하에 작업하면 gettimestamp는 Timestamp의 인스턴스를 반환합니다.이 인스턴스는 unixepoc 이후의 밀리 초 수를 long으로 제공 할 수 있습니다. Date 클래스에는 Unixepoc이 param 이후로 밀리 초 수를 나타내는 long을 취하는 생성자가 있으므로 날짜 인스턴스를 얻을 수 있습니다. – JustDanyul
여러분! 나는 그것이 효과가 있다고 생각한다. : D –