Mysql timestamp. How to handle in java - 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.

Related

How to sort leaderboard data in Firebase database for a specific date query?

I'm storing scoreboard along with date in milliseconds and query the date using startAt and endAt methods. And this is giving me all the users in that date range, now I want to sort the users based on score but I'm not able to do that.
Unfortunately, you cannot achieve this with Firebase realtime database without making some changes in the structure of your database. Unlike in Cloud Firestore, the realtime database doesn't have the capability to perform filtering on multiple properties (using "multiple WHERE clauses" as can be said in SQL terms).
If you want to check for matches on multiple properties, you'll have to create a composite property that can contain a combination of the properties that you are looking for. As an example, you'll need a string value that has the score and the date together like explained in my answer from this post.
This is a very common problem. As you've not attached you database, or your code, I can't give you the proper code of how to do this, but the best way to retrieve your users in the same order of their score can be done by a basic principle.
The basic principle is that in addition to setting the name and score in a user object, you can use setWithPriority to also set a priority for it.
Priority in this particular case can be the user's score (if its numeric, it will automatically be sorted for you). You can then use the .limit(10) query to get the list of top 10 users.
You'll also have to implement the child_added, child_changed and child_removed events to handle the cases of a new user entering the top 10, someone changing position and someone leaving the top 10 list respectively.
You can read and know more about this here: https://www.firebase.com/tutorial/#example/leaderboard

mysql compare 2 dates

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

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)

MYSQL Queries not working after I changed my timezone in my machine

I am having some strange issues for most of the my queries where I have timestamp and date fields, the queries which were working fine are not working now after I changed the timezone from PST TO IST.
Consider this query,
select SQL_CALC_FOUND_ROWS * from `mydb`.`mytable` WHERE (Date(timestamp) BETWEEN '2013-12-01' and '2012-12-16') and scpets= 'accessories' and images = 'Yes' and videoflag = 'Yes' and maps = 'Yes' and flag = 1 and title LIKE '%Book%' order by Date(timestamp) desc Limit 0, 100
I gives me 0 record back when I know there exists 2 records which meets query criteria.
Now when I remove the between clause from the queries It gives me correct records back.I am lost here as where to make the correction now as query looks perfectly fine and correct inputs are passed from my program to sql queries.All fields marked with Yes are enum columns.
Also My SimpleDataParser class which was working fine till 2 weeks back is now failing with unparseable date exception.I fixed this one,but still my above functionality is broken.
Can someone suggest me is their any relation between the change I did to my laptop(I changed my laptop's timezone from PST TO IST).I have mysql and jdk and all servers on my laptop.
Now I am scared to reverse the change back as it might broke my app completely and I really dont want to do unnecessary rework.
Ah my bad, I am searching a future dates(2013-12-01' and '2012-12-16), my bad in jquery datepicker , I added default date +365 instead of doing -365.Issue is resolved.I really dont know why keep doing silly mistakes and then wasting hours to correct those.

Using a criteria for time restriction

I'm having some problems with a simple query I'm doing.
I have a postgresql database, with a time wihout zone column.
I cannot change the type of this column. Also, I have to use criterias, so don't tell me to change this. However, if there is a better solution for the future instead of using the time type I'm curious about it.
This column is mapped to a java.util.Date attribute. This could be changed to Time or whatever.
What I'm doing is adding this :
Time someTime = someDate.getTime();
criteria.add(Restrictions.eq(propertyName, someTime)));
This criteria doesn't return anything, when it should. What is done is the following: The user puts some string time, that is parsed to Date, being that 1970-1-1 and the time. This time is then added to the criteria. Also, the time in the database I'm using as example, is the same Date (1970-1-1, etc) that the one that is created when parsing the user data.
I have tried to search for some documentation on how criterias are used against time without timezone but haven't found anything.
Suggestions why this is failing?
UPDATE:The problem has been solved using the library JodaTime. But I still don't understand why was failing...

Categories

Resources