mysql compare 2 dates - java

I'm absolute noob in Java and Mysql. Have an assignment to make where user need to login and register. All code been done. But there is one little part where the user's login should expire in 6 month. I'm trying to accomplish it by using SELECT DATEDIFF(month,'2014-06-05','2014-08-05') AS DiffDate. I can track user's registry date but with the second date I'm a bit stumped. How can I track the current time of user login. Any suggestions. Thank you.

You can use MySQL NOW() :
SELECT DATEDIFF(month,'2014-06-05',NOW()) AS DiffDate

Related

Spring scheduling on a set of dates

I'm building an auction site in Spring and Postgres where the user can list his product and other user can bid and buy (like ebay).
What I want to do is when some auction ends the system send and email to the user saying to check the page and pay. These dates are stored in the auction table and can be any date (the end date is the starting date + 30 days).
Someone knows how can I send an email on this dates (the best way to do without overload the system!)?
thanks
You can execute some job on some interval, lets say every day. In this job you query the table from the database and chek if you have to send mails for some auction.
I don't know if is the most correct way, but I follow https://stackoverflow.com/a/18896316/3237975 and everything worked as I wanted.
I just change a little bit the class CustomTask to accept an input date and done!!
Thanks for the help
(if this approach is not the best performance wise, please advice other better)

Restricting use for 5 minutes

I am planning on creating a small website with JavaEE where a user can vote but only every 5 minutes. There are two buttons. One to vote yes and the other to vote no. I want the buttons to only be clickable every 5 minutes. Whats the best way to do this?
I thought of getting the users IP and entering it into my DB along with the timestamp. Everytime a user would vote I would get their IP from the DB and check if 5 mins have passed since their last vote. What do you think of this solution? Is this best practise?
I am trying to avoid a login or any other sort of authentication.
I plan on using JSF, Java, mySQL and AJAX.
One possible solution could be a cookie containing the last date of vote in client side plus saving the user IP and date of vote in a datatabse in server side.
If there is no cookie in client side and if there is zero entry in your database containing the user's IP address, or if the date stored in this enty is inferior to your vote recover time, then the user can vote.
Keep in mind that the more people will click your buttons, the more request your server will have to handle.
There is no risk zero, but you can get close to it.

to enter a time to sqlite database using java which doesnot exist in the database before

I am using java and sqlite as back end my requirement is I have to enter a time to the database,i have 2 fields starttime and endtime in database and while registering i have to check whether that time slot is already used by another user ?
I guess for that first you have to calculate EndTime-StartTime=RequiredSlot
Then you have to run query on database to check if RequiredSlot is assign to anyone else.
Considering Same Date..

Convert Date to Time

I'm having some issues with a pre-made Java master table.
The problem is that it requires to show an inserted date from my PHPMyAdmin in the form off dd-mm-yyyy-hh-mm-ss, but instead it only shows dd-mm-yyyy. Only if I edit it in the application itself it shows me the correct form.
I know some methods to format it but I could not figure out how to use these in a premade master table.
Use DateAndTime in Java and in your PhpMyAdmin have you declare a Date and Time or just Date?

Mysql timestamp. How to handle in java

I will try to explain what I want to do.
I have a table in which I store the time that a user enters to work and update this row when the user leaves his work. The fields are timeIn and timeOut.
Before I store the timeOut, I want to display how many hours the user has worked so far. So I think I have to retrieve the timeIn and calculate the difference with the actual hour of the system. But I don't know how to retrieve only one field of a table. I reckon I have to create an object time(for example) and get the timeIn along with other parameters and then calculate the difference. But I don't know whether I'm right and how to do that.
Cheers
See here: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
You may be able to get the hours worked straight from MySQL thusly:
SELECT TIMEDIFF(NOW(), timeIn) AS hoursWorked
FROM yerTable
WHERE personIdOrWhatever...
You didn't tell us what type of field timeIn is. It should be datetime.

Categories

Resources