First, describe
1, the database has a date type of Last_login_time field, that is, each time the user login will update the field;
2. When logging in, use JdbcTemplate's queryForObject method, take out the value of the Last_login_time field returned in the date type, and format the time with DateFormat
3, get the current time of the system, and use DateFormat format, compare two time is equal to determine whether the user is logged in for the first time today.
The last time I wrote a method to determine whether the user is logged on for the first time is a string comparison, which is cumbersome and inefficient to use, so it is optimized today.
Last written blog post address: http://blog.csdn.net/tongyuehong137/article/details/38848495
Second, the source code
Get user last logon time from database//1, get user last login time using User's Get method date Dt=user.getlastlogintime ();//2, Use JdbcTemplate's queryForObject method to query the user's last logon time and convert to date type//query the last logon time from the database according to UserID string internaltimestring= "select Last_login_time from Scpn_user where user_id= "+user.getuserid ();p rivate jdbctemplate jdbctemplate;date dt= Jdbctemplate.queryforobject (internaltimestring, java.util.Date.class);//Gets the current time of the system date Time=new date (); String s;if (dt==null) {s= "";} else {s = dateformat.getdateinstance (). Format (dt);} String t= dateformat.getdateinstance (). Format (time),//formats the current system time and the user's last logon date to determine if it is the same day if (!s.equals (t)) {// Perform an add-on operation for daily login ...}
Iii. Summary
1, the way to get the last login time can be decided by themselves, in addition to the above two methods there are many ways.
2, the use of DateFormat format date instead of string comparison efficiency has improved, the use is more concise.
Java uses the DateFormat date format method to determine whether the user is logged on for the first time today (relatively simple)