title: Clock in java independent of system date and time
body: <p>I have application which stores received UDP packets in file, with storing receive time of each packet along with each packet. I am using <code>System.currentTimeMillies()</code> to get receive time when packet is received.</p>
  
  <p>I need to store time, so because, when I transmit this file on UDP, I will transmit each packets from file, synchronizing with time stored for each packet.</p>
  
  <p>The problem is, if I change system date and time while receiving and storing packets, synchronization while sending file gets messed up.</p>
  
  <p>So I need timestamping each received packet independent of system date and time. So that while sending, sending rate will be synchronized even if system date and time was changed while receiving.</p>
  
  <p>I know these can be achieved with <code>System.nanoTime()</code>, but is it safe to use this? I think <code>System.nanoTime()</code> may not be accurate over different system architecture and operating systems.</p>
  
  <p>Also, if I use <code>System.nanoTime()</code>, how this clock will start (initial value), because I do not want it to end with last value while program is running. If from the first call to <code>System.nanoTime()</code> clock starts from 0, this clock can run for around 290 years before ending with last value.</p>
  
  <p>Is there any alternative to <code>System.nanoTime()</code>? I need accuracy of clock in milliseconds.</p>
  
  <p>Also I need this clock to run independent of system, because I may be transmitting file from other machine. Also, it should not skew time for days or months, never between different systems. </p>
  
  <p>Thanks</p>