title: Getting milliseconds from date.getTime() on Android API level 19
body: <p>I need to get a date-time string in the format</p>
  
  <pre><code>"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
  </code></pre>
  
  <p>where SSS are the digits for milliseconds (from 000 to 999).</p>
  
  <p>I wrote the following code snippet:</p>
  
  <pre><code>Date date=Calendar.getInstance().getTime();
  String currentDateTimeString = (String) android.text.format.DateFormat.
         format("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Calendar.getInstance().getTime());
  </code></pre>
  
  <p>but I get strings like</p>
  
  <p>"2019-11-08T13:39:33.SSSZ"</p>
  
  <p>because of some issues as described in this answer</p>
  
  <p><a href="https://stackoverflow.com/a/58768066/930835">https://stackoverflow.com/a/58768066/930835</a></p>
  
  <p>That answer has been accepted because of the information it provides. The suggestion is not applicable to my case.</p>
  
  <p>Indeed my app needs to be compatible with Android API level 19.</p>
  
  <p>By the way this question asks for a particular code snippet to be evaluated.</p>
  
  <p>This is a standalone code snippet. So it is a different question, useful in itself.</p>
  
  <p>So I tried this code</p>
  
  <pre><code>Date date=Calendar.getInstance().getTime();
      String currentDateTimeString = (String) android.text.format.DateFormat.
             format("yyyy-MM-dd'T'HH:mm:ss",date);
      long ms=date.getTime() %1000;
      currentDateTimeString=currentDateTimeString+"."+String.valueOf(ms)+"Z";
  </code></pre>
  
  <p>but I do not know if it is reliable, that is, if it calculates the milliseconds of that Date instant correctly.</p>