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
 
Ad

Advertisements

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
 
Ad

Advertisements

Ad

Advertisements


Top