-1

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.

P5music
  • 3,197
  • 2
  • 32
  • 81
  • Possible duplicate of [Milliseconds not appearing in DateFormat.format output](https://stackoverflow.com/questions/58767796/milliseconds-not-appearing-in-dateformat-format-output) – locus2k Nov 08 '19 at 15:54
  • @locus2k No, it is about a particular code snippet with a different method of calculating milliseconds to be evaluated. Furthermore, suggestions from the other question need newer API levels than what I want. – P5music Nov 08 '19 at 16:49
  • @Sotirios Delimanolis see edits to the question and the above comment. – P5music Nov 08 '19 at 16:52

1 Answers1

2

java.time and ThreeTenABP

For Android API level 19 I suggest that you use java.time through the backport, ThreeTenABP.

Easy version:

    String currentDateTimeString = Instant.now().toString();
    System.out.println(currentDateTimeString);

Output when running just now:

2019-11-08T21:43:31.893Z

If the milliseoncds happen to be 0 (expected in one case out of 1000), they will not be printed, though. Please check if this will be a problem for you. Leaving out the milliseconds conforms with the ISO 8601 standard, so it ought to be OK, but do check in case. If you do need the milliseconds always, use a formatter:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
    String currentDateTimeString
            = OffsetDateTime.now(ZoneOffset.UTC).format(formatter);

2019-11-08T21:48:10.262Z

Would the code in the question be reliable?

but I do not know if it is reliable, that is, if it calculates the milliseconds of that Date instant correctly.

No, your code was not quite reliable. Two points:

  1. If the miilliseconds are less than 100, they will be printed in fewer than 3 digits. 58.012 seconds would be incorrectly printed as 58.12 seconds. I agree with your comment that a solution to this problem can be found.
  2. Much worse, but not related to milliseconds: Your code prints the date and time in the default time zone of the JVM (probably the device time zone setting), but appends Z, thereby claiming that the time is in UTC, which it isn’t (unless this particular device is set to UTC, but even if it were, you shouldn’t rely on that being the case always). I don’t know android.text.format.DateFormat well enough to tell whether it offers any solution to this problem.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Excuse me, the milliseconds digits issue can be solved I think. However it is risky, now it's up to me to decide. I would like to uderstand how to have the UTC time, staying with my code. Can you explain please in a comment? – P5music Nov 09 '19 at 11:52
  • @P5music I have made a minor edit, I’m afraid I can’t contribute much towards your question in the comment. – Ole V.V. Nov 09 '19 at 17:50