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