Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating elapsed time 1

Status
Not open for further replies.

Ascalonian

Programmer
Jan 4, 2008
264
US
I am trying to calculate the time it took for my validation method to start and stop. I wrote some code that I thought should have worked, but the output seems way off. My code and output is below.

Code:
long startTime = System.currentTimeMillis();
...
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

dateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
System.out.println("Start Time: " + dateFormat.format(new Date(startTime)));
System.out.println("End Time: " + dateFormat.format(new Date(endTime)));
System.out.println("Total Time: " + dateFormat.format(new Date(elapsedTime)));

And the output is:
Code:
Start Time: 09:52:04
End Time: 09:52:13
Total Time: 19:00:08

Any ideas why there is a 19 in the Total time? I would think that Total Time should be 00:00:08
 
The parameter for a java Date is the number of milliseconds since January 1, 1970, 00:00:00 (UTC). To see the correct elapsed time it has to be adjusted to UTC so that it compensates for the timezone and daylight saving time differences.

Code:
long startTime = System.currentTimeMillis();
....
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;

SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("EST"));
		
SimpleDateFormat dateFormat2 = new SimpleDateFormat("HH:mm:ss");
dateFormat2.setTimeZone(TimeZone.getTimeZone("UTC"));
		
System.out.println("Start Time: " + dateFormat.format(new Date(startTime)));
System.out.println("End Time: " + dateFormat.format(new Date(endTime)));
System.out.println("Total Time: " + dateFormat2.format(new Date(elapsedTime)));

Code:
Start Time: 10:30:08
End Time: 10:30:10
Total Time: 00:00:02
 
Awesome! Thank you for the tip. It worked great.

Have a star!
 
You may also like to know that if you are using Java6 there is a tool in the java install bin folder called jvisualvm.exe. This can profile all your code to see where the time is being spent, and view the threads and memory usage.

Tim
 
timw,
Thank you for that tip as well. Unfortunately, I need this during run time and need to display it to the users.
 
you might just do something like:
<code>
long elapsedTime = endTime - startTime;
int seconds = elapsedTime / 1000;
System.out.println ("seconds " + seconds);
</code>

Is this value ever going to exceed 60 seconds?
This might not be a problem until seconds
gets quite large i.e. >120 sec. even then a simple

<code>
long elapsedTime = endTime - startTime;
seconds = elapsedTime / 1000;
minutes = seconds / 60;
seconds %= 60;
System.out.println ("" + minutes + ":" + seconds);
</code>

gets you where you want. Simple and easy for the next
guy to read without JavaDoc lookups.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top