Results 1 to 4 of 4
  1. #1
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Cool getting time elapsed in seconds

    What I want is time elapsed b/w 2 times.

    My requirement is terminating a loop if 5 seconds have elapsed.

    Thanx.

    Ali.

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Java Code:
    final int seconds = 5;
    final long start = System.currentTimeMillis();
    // Start loop
    while (true) {
        // Do stuff
        if (System.currentTimeMillis()-start >= seconds*1000)
            break; // Stop loop
    }
    // End loop
    There you go.
    Of course you also could use a Timer or Thread, but this is easiest. :)
    I die a little on the inside...
    Every time I get shot.

  3. #3
    ali_sakar is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    Thanx a lot buddy...

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    You're welcome.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Need help with calculate the number of seconds
    By ProUnbeatable in forum New To Java
    Replies: 5
    Last Post: 10-11-2008, 03:07 AM
  2. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  3. Replies: 1
    Last Post: 06-30-2008, 07:14 AM
  4. Replies: 0
    Last Post: 06-26-2008, 07:32 PM
  5. measuring time in nano seconds
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-06-2007, 12:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •