File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Stop Watch" Watch "Stop Watch" New topic

Stop Watch

Thorsten Schenkel

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

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
I agree. Here's the link:
subject: Stop Watch
It's not a secret anymore!