Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stop Watch

 
Thorsten Schenkel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic