CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2010
    Posts
    121

    how do you display time correctly using System.currentTimeMillis();

    I am using a timer routine which I obtained from the net. This is the code:
    Code:
    long start = System.currentTimeMillis();
    textAreaGameInfo.append(strBuf.toString());
    long elapsed = System.currentTimeMillis() - start;
    How can I force the correct display of time as follows:
    If 0 seconds or less it should show 0.142 milliseconds
    If 0 seconds or greater it should show 1.42 seconds
    If 1 minute or greater it should show 1min 42 seconds

    Thanks
    Last edited by peahead; April 6th, 2010 at 03:17 PM. Reason: typo

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: how do you display time correctly using System.currentTimeMillis();

    Quote Originally Posted by peahead View Post
    If 0 seconds or less it should show 0.142 milliseconds
    You won't get thousandths of a millisecond from System.currentTimeMillis()

    Once you get your units/decimal places sorted out, you can use SimpleDateFormat.

    Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable techniques to solve it...
    A. Aho and J. Ullman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2010
    Posts
    12

    Re: how do you display time correctly using System.currentTimeMillis();

    you may use :
    double start = System.currentTimeMillis();
    //your program...
    double stop = System.currentTimeMillis();

    System.out.print((stop-start)+" Millisecond")
    //or
    System.out.print((stop-start)/1000+" Seconds");

  4. #4
    Join Date
    Feb 2010
    Posts
    121

    Re: how do you display time correctly using System.currentTimeMillis();

    Thank you dustu pagla - works great

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured