find difference in date/time variables

Discussion in 'Java' started by Drew, Jan 25, 2006.

  1. Drew

    Drew Guest

    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
     
    Drew, Jan 25, 2006
    #1
    1. Advertising

  2. Drew

    Vova Reznik Guest

    Drew wrote:
    > 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");
     
    Vova Reznik, Jan 25, 2006
    #2
    1. Advertising

  3. Drew

    Drew Guest

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

    Drew



    On Wed, 25 Jan 2006 15:36:25 GMT, Vova Reznik <>
    wrote:

    >Drew wrote:
    >> 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");
     
    Drew, Jan 25, 2006
    #3
  4. Drew

    Vova Reznik Guest

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


    Google java nanoseconds
     
    Vova Reznik, Jan 25, 2006
    #4
  5. Vova Reznik wrote:
    > Drew wrote:
    >
    >> 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
     
    Babu Kalakrishnan, Jan 25, 2006
    #5
  6. Drew

    Roedy Green Guest

    On Wed, 25 Jan 2006 11:42:04 -0500, Drew <> wrote, quoted
    or indirectly quoted someone who said :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 25, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    1
    Views:
    619
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,295
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    531
    Pete Becker
    Nov 8, 2003
  4. Lad
    Replies:
    2
    Views:
    294
    Klaus Alexander Seistrup
    May 24, 2006
  5. Nico Grubert
    Replies:
    2
    Views:
    335
    Klaus Alexander Seistrup
    May 24, 2006
Loading...

Share This Page