Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Stop Watch

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
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.
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.
Replace the word "snake" with "danger noodle" in all tiny ads.
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic