Monday, May 24, 2010

How to calculate the time difference in Java?

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!

How to calculate the time difference in Java?
instead of writing a sample out here, here's one on the net





http://www.java2s.com/Code/Java/Developm...
Reply:KidK has given awesome solution. Report It

Reply: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...
Reply: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.
Reply: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(timeI...


Date timeOutD = DateFormat.getDateInstance().parse(timeO...





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;


No comments:

Post a Comment