Java DateTimeFormatter 在时区为 +0000 时添加 Z

Java DateTimeFormatter adding a Z when timezone is +0000

假设我有两个日期:´

我将 dateTimeFormatter 与 SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.getDefault());

一起使用

最后我得到了这个:

此外,我不能只用 +00:00 替换 Z,因为那样会多一个处理步骤。

完整代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX", Locale.getDefault());
this.timestampString = sdf.format(date);    

当您查看 the Wikipedia Page for ISO 8601 时,您会发现这是预期的行为:

Coordinated Universal Time (UTC)

If the time is in UTC, add a Z directly after the time without a space. Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".

The Z suffix in the ISO 8601 time representation is sometimes referred to as "Zulu time" because the same letter is used to designate the Zulu time zone. However the ACP 121 standard that defines the list of military time zones makes no mention of UTC and derives the "Zulu time" from the Greenwich Mean Time[28] which was formerly used as the international civil time standard. GMT is no longer precisely defined by the scientific community and can refer to either UTC or UT1 depending on context.[29]

任何客户端都应该能够解析它。