File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Comparing two times Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing two times" Watch "Comparing two times" New topic
Author

Comparing two times

Kerry Shannon
Ranch Hand

Joined: May 08, 2001
Posts: 31
Creating a time entry screen for payroll. Trying to write the code to compare two (or more) times entered in the form (JSP/servlet) to determine the difference. I did not examples where you start with a form input string and compare to compute a difference in hours and minutes.
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Use DateFormat.parse(String text) to turn the form entries into Date objects. Date.getTime() will return a value of the time in milliseconds. You can compare the times to get the difference, then convert the milliseconds into minutes.
Kerry Shannon
Ranch Hand

Joined: May 08, 2001
Posts: 31
We will try your suggestions.
Editorial comment: I wish it was easier to work with dates, and I wish there were more examples of dates to strings and strings to dates. These are not new ideas. Someone has already done this.
Kerry Shannon
Ranch Hand

Joined: May 08, 2001
Posts: 31
IT WORKS!!! We were able to do what you told us to do.
Thank you!
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Can you please tell me how you did it because when i tried to use
java.util.Date date1 = java.text.DateFormat.parse(args[0])
it gives an error
Can't make static reference to method java.util.Date parse(java.lang.String) in class java.text.DateFormat.
java.util.Date date1 = java.text.DateFormat.parse(args[0]);


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Kerry Shannon
Ranch Hand

Joined: May 08, 2001
Posts: 31
We have a form coming in from the employee who entered StartTime.
First, we display from the database if it was previously saved:
if (time.getDStartTime() != null)
{ StartTime = tmShort.format(time.getDStartTime()); }

Then, we paint the time entry form:
<td ><input type="text" name="StartTime" value="<%=StartTime%>"></td>
Then, we used the suggestion to get the value of the time they entered:
<%<br /> long starttime = time.getDStartTime().getTime();
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Parmeet, you need to create an instance of the DateFormat class first. The parse method is not static. You can use the getDateInstance(), getTimeInstance(), or getDateTimeInstance() methods. The API explains all of these options in detail.

http://java.sun.com/j2se/1.3.0/docs/api/java/text/DateFormat.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing two times