Camus
Camus

Reputation: 846

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

Upvotes: 0

Views: 239

Answers (1)

Andrew
Andrew

Reputation: 4624

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.

Upvotes: 1

Related Questions