如何获取 Java 中 UTC+0 的日期?
How to get Date in UTC+0 in Java?
我正在使用以下代码获取 ISO-8601 格式的日期。对于 UTC,返回的值不包含偏移量。
OffsetDateTime dateTime = OffsetDateTime.ofInstant(
Instant.ofEpochMilli(epochInMilliSec), zoneId);
return dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
对于其他时间格式,返回的响应如下所示:
2016-10-30T17:00:00-07:00
如果是 UTC,则返回值为:
2016-10-30T17:00:00Z
我希望它是:
2016-10-30T17:00:00+00:00
注意:不要使用 UTC-0,因为 -00:00 不符合 ISO8601。
当偏移量为零时,内置格式化程序使用 Z
。 Z
是 Zulu
的缩写,意思是 UTC.
您必须使用自定义格式化程序,使用 java.time.format.DateTimeFormatterBuilder
设置偏移量为零时的自定义文本:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date and time, use built-in
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// append offset, set "-00:00" when offset is zero
.appendOffset("+HH:MM", "-00:00")
// create formatter
.toFormatter();
System.out.println(dateTime.format(fmt));
这将打印:
2016-10-30T17:00:00-00:00
只是提醒一下 -00:00
不是 ISO8601 compliant。当偏移量为零时,标准仅允许 Z
和 +00:00
(以及变体 +0000
和 +00
)。
如果要+00:00
,只需将上面的代码改成:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date and time, use built-in
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// append offset
.appendPattern("xxx")
// create formatter
.toFormatter();
此格式化程序将产生输出:
2016-10-30T17:00:00+00:00
如果你能接受+00:00
而不是-00:00
,你也可以使用更简单的DateTimeFormatter
:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxxx");
OffsetDateTime odt = OffsetDateTime.parse("2016-10-30T17:00:00Z");
System.out.println(fmt.format(odt));
我使用 x
而 OffsetDateTime
的标准 toString()
方法使用 X
。 x
和 X
之间的主要区别在于 return +00:00
与 Z
另一个
我正在使用以下代码获取 ISO-8601 格式的日期。对于 UTC,返回的值不包含偏移量。
OffsetDateTime dateTime = OffsetDateTime.ofInstant(
Instant.ofEpochMilli(epochInMilliSec), zoneId);
return dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
对于其他时间格式,返回的响应如下所示:
2016-10-30T17:00:00-07:00
如果是 UTC,则返回值为:
2016-10-30T17:00:00Z
我希望它是:
2016-10-30T17:00:00+00:00
注意:不要使用 UTC-0,因为 -00:00 不符合 ISO8601。
当偏移量为零时,内置格式化程序使用 Z
。 Z
是 Zulu
的缩写,意思是 UTC.
您必须使用自定义格式化程序,使用 java.time.format.DateTimeFormatterBuilder
设置偏移量为零时的自定义文本:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date and time, use built-in
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// append offset, set "-00:00" when offset is zero
.appendOffset("+HH:MM", "-00:00")
// create formatter
.toFormatter();
System.out.println(dateTime.format(fmt));
这将打印:
2016-10-30T17:00:00-00:00
只是提醒一下 -00:00
不是 ISO8601 compliant。当偏移量为零时,标准仅允许 Z
和 +00:00
(以及变体 +0000
和 +00
)。
如果要+00:00
,只需将上面的代码改成:
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
// date and time, use built-in
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
// append offset
.appendPattern("xxx")
// create formatter
.toFormatter();
此格式化程序将产生输出:
2016-10-30T17:00:00+00:00
如果你能接受+00:00
而不是-00:00
,你也可以使用更简单的DateTimeFormatter
:
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxxx");
OffsetDateTime odt = OffsetDateTime.parse("2016-10-30T17:00:00Z");
System.out.println(fmt.format(odt));
我使用 x
而 OffsetDateTime
的标准 toString()
方法使用 X
。 x
和 X
之间的主要区别在于 return +00:00
与 Z
另一个