Timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rpm27
    New Member
    • Jan 2008
    • 23

    #1

    Timer

    Hi, I have written a solitaire game (a very small one) but I want to add a timer which will run while i'm playing and will give me the time spent on the game. I imagine it will need multithreading. I'm I right?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Well, you can use the Date class's getTime method, called when you start and when you end. From there, simply subtract and convert to minutes and seconds (there's probably a builtin way to do this, some sort of formatter (DateFormat?), but I'm not entirely sure). No multithreading needed.

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      Originally posted by Laharl
      Well, you can use the Date class's getTime method, called when you start and when you end. From there, simply subtract and convert to minutes and seconds (there's probably a builtin way to do this, some sort of formatter (DateFormat?), but I'm not entirely sure). No multithreading needed.
      Then i agree....

      @ OP, if you also like to automatically update the timer... You can also use multithreading implementation. ... With a reasonable infinite loop....

      Take a look at this thread.

      I hope you could also get some idea there...

      sukatoa....

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by rpm27
        Hi, I have written a solitaire game (a very small one) but I want to add a timer which will run while i'm playing and will give me the time spent on the game. I imagine it will need multithreading. I'm I right?
        I'm afraid that's vague. If you want to compute the elapsed time, use System.currentT imeMillis:

        [CODE=Java]long start = System.currentT imeMillis();
        //do stuff
        long elapsed = System.currentT imeMillis() - start;[/CODE]

        If, on the other hand, you want to display a clock ticking away in your Swing GUI, use javax.swing.Tim er:

        This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components


        If you want to do something else, please try to express yourself more clearly.

        Comment

        • rpm27
          New Member
          • Jan 2008
          • 23

          #5
          Thank you for your answers. the Timer seems to be the most interesting solution, I'll check the API documentation right away. Thanks again.

          Comment

          Working...