title: Java : Custom Timestamp Format : verify format to microsec precision
body: <p>My Objective is
  to create a java class that can handle the below two requirements</p>
  
  <p><strong>(A) 1. Verify if the format of a timestamp matches with expected format.</strong><br>
  CCYY-MM-DD'T'hh:mm:ss'.0000000000+'uh:um"<br>
  Ex: the expected format is not static. </p>
  
  <p>It may be either of these
      "2013-09-10T18:30:20.123456+10:00" or
      "2013-09-10T18:30:20.123+10:00".<br>
  I am not bothered about the
      precision and value. Only the format matters.    </p>
  
  <p><strong>(B)  2. Verify if the timestamp is in a certain range.</strong><br>
  Ex: Verify if the timestamp is in
      between "2013-09-10 18:27" and "2013-09-10 18:33". (verification is only upto minute level precision) (may be a delta of + or - 2min)</p>
  
  <hr>
  
  <blockquote>
    <p>As suggested by one of the member, I have edited the post to target at
    One specific question.</p>
  </blockquote>
  
  <p><em><strong>The QUESTION :</em></strong> </p>
  
  <p>How to validate the custom timestamp upto microsec precision using JAVA class ?</p>
  
  <p>The two arguments for this class will be </p>
  
  <p>1) Expected FORMAT as a String </p>
  
  <p>2) timestamp value as a String</p>
  
  <p>Based on analysis from various search results, below is my understanding :</p>
  
  <ol>
  <li>Java (by default) does not parse/format Timestamp at microsecond level( I used SimpleDateFormat)</li>
  <li>If 6 digits are given in milliseconds place, it will re-calculate the value into seconds and the dateformat will be updated and the new dateformat will have 3 digits in milliseconds precision.</li>
  <li>I have also seen a thread which suggests to use java.sql.Timestamp.  </li>
  </ol>
  
  <p>Tried this approach but not working.
  I was not able to convert my strTimestamp 2013-09-10T18:30:20.123456+10:00 into Timestamp object. </p>
  
  <pre><code>Timestamp ts = Timestamp.valueOf(strTimestamp); 
  java.lang.IllegalArgumentException: 
   Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
  </code></pre>
  
  <p>I was not able convert my input format into Timestamp object.</p>
  
  <hr>
  
  <p>I have a workaround to validate using regular expression : </p>
  
  <pre><code>2013-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T(0[0-9]|1[0-9]|2[0-3]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9]).[0-9][0-9][0-9][0-9][0-9][0-9]\+10:00
  </code></pre>
  
  <p>The problem with this reg ex is that, my expected timestamp format is not static. So i have to use a regex for every pattern.</p>
  
  <p><strong>So I am trying to figure out if there is any robust solution in java, which can be self sufficient even if the expected format changes.</strong></p>