gpt4 book ai didi

java - 毫秒未出现在 DateFormat.format 输出中

转载 作者:行者123 更新时间:2023-12-01 21:49:55 26 4
gpt4 key购买 nike

我想要一个“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 标准类 LocalDateTimeDateTimeFormatter,它们确实支持毫秒。

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

[已编辑]

重新阅读问题后,我认为问题在于您使用的是 DateFormat 而不是 SimpleDateFormat。您提供的链接与您在代码中使用的实际类不对应。

关于java - 毫秒未出现在 DateFormat.format 输出中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58767796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com