find difference in date/time variables

D

Drew

Hi All:

I have an existing Java application which works fine. I want to add
some code to the application just to calculate the run time of the
program.

Its a Java Console program.

So, at the very start of the program, I want to get the current
date/time and store it in a variable.

Then, at the very last line of the program, I will again get the
current data/time and store it in a variable.

What's the best way to get the current date/time in Java and what's
the easiest way to find the difference in time in the two dates?

My existing application may sometimes take a few seconds and other
times runs in less than a second. So, my output probably needs to go
down to the millisecond level.

Any help is much appreciated!

Thanks!
Drew
 
V

Vova Reznik

Drew said:
Hi All:

I have an existing Java application which works fine. I want to add
some code to the application just to calculate the run time of the
program.

Its a Java Console program.

So, at the very start of the program, I want to get the current
date/time and store it in a variable.

Then, at the very last line of the program, I will again get the
current data/time and store it in a variable.

What's the best way to get the current date/time in Java and what's
the easiest way to find the difference in time in the two dates?

My existing application may sometimes take a few seconds and other
times runs in less than a second. So, my output probably needs to go
down to the millisecond level.

Any help is much appreciated!

Thanks!
Drew
long start = System.currentTimeMillis();
....
long finish = System.currentTimeMillis();
System.out.println("It took " + (finish - start) + " millis to exec");
 
D

Drew

Thanks Vova! This works perfectly! Is milliseconds the smallest unit
of time supported in Java?

Drew
 
B

Babu Kalakrishnan

Vova said:
Google java nanoseconds

This might point to the System.nanoTime() method - but you should also
read the fine print (the API documentation) which specifies that the
accuracy and granularity of this value is not guaranteed and is platform
specific. On my Centrino Laptop (with JDK 1.5.0_04) I haven't seen any
values returned by this method that dont end in a "000" (Which implies
at best a microsecond granularity whereas I would have expected better
if the method were using the TSC counter of the CPU - i.e. the RDTSC
instruction)

BK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top