Java uses the DateFormat date format method to determine whether the user is logged on for the first time today (relatively simple)

Source: Internet
Author: User
Tags dateformat

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.