DateFormat.format 输出中未出现的毫秒数

Milliseconds not appearing in DateFormat.format output

我想要 "yyyy-MM-dd'T'HH:mm:ss.SSSZ" 格式的日期时间字符串。 我写了下面的代码片段:

    Date date=Calendar.getInstance().getTime();
    String currentDateTimeString = (String) android.text.format.DateFormat.
           format("yyyy-MM-dd'T'HH:mm:ss.SSSZ",Calendar.getInstance().getTime());

但我得到的字符串像

"2019-11-08T13:39:33.SSSZ"

当格式为 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" 时('Z' 转义)。

模式位于 https://developer.android.com/reference/java/text/SimpleDateFormat.html#examples

为什么没有出现毫秒?

我对Android开发知之甚少,但快速搜索文档可能会给你答案:

https://developer.android.com/reference/android/text/format/DateFormat

The format methods in this class implement a subset of Unicode UTS #35 patterns. The subset currently supported by this class includes the following format characters: acdEHhLKkLMmsyz. Up to API level 17, only adEhkMmszy were supported. Note that this class incorrectly implements k as if it were H for backwards compatibility.

Unicode UTS #35 没有提到毫秒: https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns

所以好像不支持格式化毫秒

我宁愿使用 Java 标准 classes LocalDateTimeDateTimeFormatter,它们支持毫秒。

https://developer.android.com/reference/java/time/format/DateTimeFormatter.html

[已编辑]

重新阅读问题后,我认为问题在于您使用的是 DateFormat 而不是 SimpleDateFormat。您提供的 link 与您在代码中使用的实际 class 不一致。