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
posted
0
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
posted
0
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
posted
0
IT WORKS!!! We were able to do what you told us to do. Thank you!
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
posted
0
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.