毫秒未出现在DateFormat.format输出中 [英] Milliseconds not appearing in DateFormat.format output

查看:178
本文介绍了毫秒未出现在DateFormat.format输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个日期时间字符串,格式为 yyyy-MM-dd’T’HH:mm:ss.SSSZ。
我编写了以下代码段:

I want to have a date-time string in the "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format. 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());

但是我得到的字符串是

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

格式为 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'(转义'Z')时也是如此。

also when the format is "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" (with 'Z' escaped).

https://developer.android.com/reference/java/中找到text / SimpleDateFormat.html#examples

为什么不显示毫秒?

推荐答案

我对Android开发一无所知,但是快速搜索文档可能会为您提供答案:

I know little about Android development, but a quick search in the documentation may give you the answer:

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


此类中的format方法实现了Unicode UTS#35模式的子集。此类当前支持的子集包含以下格式字符:acdEHhLKkLMmsyz。直到API级别17,仅支持adEhkMmszy。请注意,此类为了向后兼容而错误地将k当作H来实现。

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没说毫秒。 b $ b https://unicode.org/reports/tr35/tr35-dates .html#Date_Format_Patterns

The Unicode UTS #35 says nothing about milliseconds: https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns

所以似乎不支持格式毫秒设置。

So it seems that formatting milliseconds is not supported.

而是使用Java标准类 LocalDateTime DateTimeFormatter 来支持毫秒。

I would rather use the Java standard classes LocalDateTime and DateTimeFormatter, which do support milliseconds.

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

[已编辑]

重新输入后阅读问题,我认为问题是您使用的是 DateFormat 而不是 SimpleDateFormat 。您提供的链接与您在代码中使用的实际课程不符。

After re-reading the question, I think the problem is that you're using DateFormat instead of SimpleDateFormat. The link you've provided does not correspond with the actual class you're using in your code.

这篇关于毫秒未出现在DateFormat.format输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆