Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

find difference in date/time variables

Thread view: 
Drew - 25 Jan 2006 15:22 GMT
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
Vova Reznik - 25 Jan 2006 15:36 GMT
> Hi All:
>
[quoted text clipped - 21 lines]
> Thanks!
> Drew

long start = System.currentTimeMillis();
...
long finish = System.currentTimeMillis();
System.out.println("It took " + (finish - start) + " millis to exec");
Drew - 25 Jan 2006 16:42 GMT
Thanks Vova!  This works perfectly!  Is milliseconds the smallest unit
of time supported in Java?

Drew

>> Hi All:
>>
[quoted text clipped - 26 lines]
>long finish = System.currentTimeMillis();
>System.out.println("It took " + (finish - start) + " millis to exec");
Vova Reznik - 25 Jan 2006 16:52 GMT
> Thanks Vova!  This works perfectly!  Is milliseconds the smallest unit
> of time supported in Java?

Google java nanoseconds
Babu Kalakrishnan - 25 Jan 2006 18:04 GMT
>> Thanks Vova!  This works perfectly!  Is milliseconds the smallest unit
>> of time supported in Java?
>
> 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
Roedy Green - 25 Jan 2006 20:07 GMT
>Thanks Vova!  This works perfectly!  Is milliseconds the smallest unit
>of time supported in Java?

see http://mindprod.com/jgloss/time.html
for your options for finer resolution.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.