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

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

@foreach($f_updt as $x => $f_updts)

   @foreach($f_updts as $f_updts1)
      @if(isset($f_updts1))
        @if(isset($f_updts1->id))

            <div class="post00">
                <p>Other posts</p>
                <a href="/sharp/posts/{{ $f_updts1->id }}">
                    <h3> {{$x}}  </h3>
                    <br>
                    <h4> {{$f_updts1->title}}  </h4>
                    <br>
                    <h5> {{$f_updts1->body}}  </h5>
                </a>
            </div>
            <br>
          @endif
        @endif
    @endforeach
@endforeach

但是我得到像这样的字符串

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

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

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

为什么不显示毫秒?

niugao 回答:毫秒未出现在DateFormat.format输出中

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

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

  

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

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。您提供的链接与您在代码中使用的实际类不符。

本文链接:https://www.f2er.com/3137473.html

大家都在问