title: java.sql.Timestamp way of storing NanoSeconds
body: <p>java.sql.Timestamp constructor go like this:</p>
  
  <pre><code>public Timestamp(long time) {
      super((time/1000)*1000);
      nanos = (int)((time%1000) * 1000000);
      if (nanos &lt; 0) {
          nanos = 1000000000 + nanos;     
          super.setTime(((time/1000)-1)*1000);
      }
  }
  </code></pre>
  
  <p>It basically accepts time in millisecond and then extracts the last 3 digits and makes it nanos. So for a millisecond value of 1304135631 <strong>421</strong>, I'm getting Timestamp.getnanos() as 
  <strong>421000000</strong>. This is plain calculation (adding 6 zeroes at the end)... does not seems to be optimum. </p>
  
  <p>A better way could have been Timestamp constructor that accepts time in nanoseconds and then calculates the nanosecond value out of that.</p>
  
  <p>If you run the below program, you'll see the difference between actual nanoseconds and the one returned by Timestamp way of calculating nanosecods. </p>
  
  <pre><code>long a = System.currentTimeMillis();
      for(;;){
          long b = System.currentTimeMillis();
          Timestamp tm = new Timestamp(System.currentTimeMillis());
          System.out.println(tm.getTime());
          System.out.println(tm.getNanos());
          System.out.println("This is actual nanos" + System.nanoTime()%1000000000);
          System.out.println("--------------------------");
          if(b-a &gt;= 1)
              break;
      }
  </code></pre>
  
  <p>So all the discussion about Timestamp that says it stores time up to nanoseconds , does not seems to be so correct.. Isn't?</p>