This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Stop Watch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stop Watch" Watch "Stop Watch" New topic
Author

Stop Watch

Thorsten Schenkel
Greenhorn

Joined: Jul 02, 2002
Posts: 7
I want to implement a stop watch.
Because of that is must subtract the current stop time from the start time. But this does not work in my example. The difference between start and stop time is always one hour too big.
What is wrong?
Here is the example:

kind regards
Thorsten Schenkel
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
The classes that you're using to format your time may be a little too powerful for what you need. I've tried something similar with timers and found that it's difficult to do simple time conversions using the stock Java time/date objects.
Why don't you just add some simple methods to your program that convert milliseconds to seconds. You could then determine the number of minutes, or hours or whatever it took your program to run. This would be pretty easy to write and would save you a lot of conversion headaches that come with using some of the objects you're trying to use.
Check out the online (free!) gook "How To Think Like A Computer Scientist: The Java Edition" for info on how to create your own time classes.


Tom Purl<br />SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The problem is here:
new Date(diffTime)
If you look at the Date constructor's API, you will find that a long argument is interpreted as a number of milliseconds after midnight Jan 1, 1970, GMT. Since you're evidently at GMT+1 this works out to just after 0100. ("Just after" since datediff if small.)
I suggest you use Calendar to set the time to midnight local time (any date), and then add datediff milliseconds. Then you can use the same formatter to get the format you want.


"I'm not back." - Bill Harding, Twister
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Stop Watch
 
Similar Threads
throw out-of-memory error
Performance wrt multi threaded approach with Thread Pool vs Sequential execution
Programming Class
oddness while question.
Updating GUI properties