Check whether the date changed [closed]

2019-09-21 16:48发布

I am working on an attendance project in Java Swing. Log in should be only once a day.

My project contains of login page which has a database from which user names and passwords are given, I just want to make sure that login (attendance updated for an employee) takes place only once a day.

How to check whether the date changed?

3条回答
Rolldiameter
2楼-- · 2019-09-21 17:26

Does it really matter if someone logs in more than once a day? Actually, your requirement has a spoofing problem. Suppose that a coworker stole my password, and he used it to log in as me. Then I come in, and I now cannot log in. It's better to allow users to log in whenever they want, and to show a message "You last logged in at 8:45 AM EDT on 3 June 2013".

If you need this exact requirement, store the last login date in the database as a SQL DATE type, which does not have time-of-day. Have the login code look like

PreparedStatement ps = conn.prepareStatement(
    "SELECT last_logon FROM USERS WHERE username = ? AND passwordHash = ?");
// Please don't store the password itself in the database.
ps.setString(1, username);
ps.setString(2, sha1(password, salt)); // very vague, I know.
ResultSet rs = ps.executeQuery();
if (rs.next()) {
    java.sql.Date lastLogin = rs.getDate("last_logon");
    // Check that last_logon isn't today.
    // Use an UPDATE to update the field.
}

This really isn't Swing-specific, or even Java-specific. You'll want to look at how your database can compute message hashes, and how you can apply salt to the passwords. You'll need to decide the salt source.

查看更多
混吃等死
3楼-- · 2019-09-21 17:44

If you're using a Java Calendar, you can also just store last login, then compare to current one:

 Calendar lastLogin = // however you set this
 Calendar currentLogin = // now, again, however you set it
 if (currentLogin.get(Calendar.DAY_OF_YEAR) == lastLogin.get(Calendar.DAY_OF_YEAR))
 {
   // it's the same day
 }
查看更多
走好不送
4楼-- · 2019-09-21 17:45

Create a database table for storing the log data of login data and have a columns userid, date and login information. And you can program if login value is 0 then the person didn't login otherwise he logged in. If you are clever enough, login column is not needed even. Now, if a user tries to log in, check if the record exits in the table against that user for that day.

That's all

查看更多
登录 后发表回答