English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have to know the total no. of hours an employee has rendered thru the employee's time-in and time-out (codes in Java).

Data type of time-in and time-out is
DATETIME (e.g. yyyy-MM-dd HH:mm)

Pls. help me.

Tnx!

2006-07-19 18:39:18 · 4 answers · asked by seraphicmortal 2 in Computers & Internet Programming & Design

Hi Tetris,

My Time-in and Time-out is already parsed and it's not a string... it's datatype as I've mentioned is DATETIME.

2006-07-19 19:11:24 · update #1

4 answers

instead of writing a sample out here, here's one on the net

http://www.java2s.com/Code/Java/Development-Class/DateDiffcomputethedifferencebetweentwodates.htm

2006-07-19 18:55:00 · answer #1 · answered by KidK 2 · 1 0

seraphicmortal, if you are using a data type with the name DATETIME then it could be a proprietory type. This means, it is not a standard Java type. The types in Java are
(1) an sql Date
(2) a Date (this is mostly deprecated)
(3) a Calendar.

You will have to check the source, API docs, etc of this data type, and check whether there are any methods to get a 'long' or similar that represents the time, and then subtract these two 'long's that represent the two times, and convert the result back to minutes, hours, days, etc.

Most times should be saved in an integer form that starts from 1970 or so. This makes the dates standardised, and you can compare them.

You could even post the source or API docs on here in the additional details, and then someone maybe able to help you find the method(s) if any exist.

2006-07-21 14:34:44 · answer #2 · answered by Mark aka jack573 7 · 1 0

Oops. I noticed that Yahoo truncated part of my source because it was too long. Be sure you put your cursor over the code containing DateFormat to examine the rest of it! It's shown as a tooltip.

---

Hi, again, thanks for the note. As mentioned below (thanks), DATETIME isn't a standard Java class. Can you provide me an interface definition of it, or can you convert it to a standard Java Date object or can you convert it to a string? It's also possible to use the DateFormat class and specify a custom date type, but you will need to convert your DATETIME class objects all in to Strings.

--

Hi,

Using the Date class (java.util.Date), you can manipulate time, and using DateFormat, we can parse the time. Let's say that you have your date format as a String:

String timeIn;
String timeOut;

And now we want the difference of the time:

Date timeInD = DateFormat.getDateInstance().parse(timeIn);
Date timeOutD = DateFormat.getDateInstance().parse(timeOut);

long msec = timeOutD.getTime() - timeInD.getTime();

msec should now contain the time, in milliseconds, between your time-in and time-out.

To convert it to something useful:

float timeHours = msec / 1000 / 60 / 60;

2006-07-20 01:59:16 · answer #3 · answered by Tetris Otaku 3 · 0 0

i've written one program but it is in VB. you need to import a date class. also, you need to manipulate the date format so java understands it. i believe you are using mysql as database right? (yyyy-MM-dd HH:mm:ss)

i think java's date format is dd-MM-YYYY, this is the same in VB. i used a string to manipulate the date format so it becomes like this:

Dim adate As Date
Dim strDate As String
adate = Date.Now
strDate = adate.ToString("yyyy-MM-dd HH:mm:ss")

from here, you simply subtract your time in from your time out..
VB has this command to calculate the number of hours from 2 input date parameters:

sstring = DateDiff("H", ddate, sdate, FirstDayOfWeek.Monday)

where "H" stands for "Hours" (the result is in hours)
ddate as your date in
sdate as your date out
and firstdayofweek is the start of week, since it is office use, we use monday as firstdayofweek

its been 3 years since i last programmed in java so i kinda forgot it! i hope this gives you some idea though...

2006-07-20 04:29:56 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers