Click to See Complete Forum and Search --> : how do you display time correctly using System.currentTimeMillis();


peahead
April 6th, 2010, 03:16 PM
I am using a timer routine which I obtained from the net. This is the 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

dlorde
April 6th, 2010, 03:44 PM
If 0 seconds or less it should show 0.142 milliseconds

You won't get thousandths of a millisecond from System.currentTimeMillis() :D

Once you get your units/decimal places sorted out, you can use SimpleDateFormat (http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html).

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

dustu_pagla
April 7th, 2010, 04:10 AM
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");

peahead
April 7th, 2010, 02:34 PM
Thank you dustu pagla - works great