title: Getting precise elapsed time using Java to the Millisecond
body: <p>I've been searching for an answer to my dilemma and have found some useful tips but nothing that addresses my specific question, so I was hoping someone here might be able to help me out.</p>
  
  <p>I'm trying to get a precise elapsed time to the millisecond in Java.  I'm using System.nanoTime() to get the current time and implementing it in the following code.  Mind you, this was code that I used to test it's precision.</p>
  
  <blockquote>
    <p>long startTime = System.nanoTime()/1000000;<br>
    while (true)<br>
    {<br>
        System.out.println((System.nanoTime()/1000000)-startTime);<br>
        }  </p>
  </blockquote>
  
  <p>A portion of the output looks like this.  </p>
  
  <blockquote>
    <p>1110<br>
    1112<br>
    1112<br>
    1113<br>
    1114<br>
    1118<br>
    1120  </p>
  </blockquote>
  
  <p>The first digit is the number of seconds, the second, tenths of a second, the third, hundredths, and then the last is thousandths.  You can't see it here, but I have precision to the hundredths place - no skipped or repeated numbers.  The thousandths, though, is far from precise.  0 -> 2 -> 2 -> 3 -> 4 -> 8 -> 0.  For what I'm doing, I need precision to the thousandths place and from all I have read it seems like System.nanoTime() should be able to provide precision to the millisecond.</p>
  
  <p>Am I doing something wrong or is there another way I can get precision to the millisecond?</p>