OffsetDateTime-打印偏移量而不是Z [英] OffsetDateTime - print offset instead of Z
本文介绍了OffsetDateTime-打印偏移量而不是Z的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有此代码:
String date = "2019-04-22T00:00:00+02:00";
OffsetDateTime odt = OffsetDateTime
.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME)
.withOffsetSameInstant(ZoneOffset.of("+00:00"));
System.out.println(odt);
此打印:2019-04-21T22:00Z
如何打印2019-04-21T22:00+00:00
?用偏移量代替Z
.
How can I print 2019-04-21T22:00+00:00
? With offset instead of Z
.
推荐答案
在标准库中,没有任何静态DateTimeFormatter
执行此操作.
它们默认为Z
或GMT
.
None of the static DateTimeFormatter
s do this in the standard library.
They either default to Z
or GMT
.
要获得无偏移的+00:00
,您将必须构建自己的DateTimeFormatter
.
To achieve +00:00
for no offset, you will have to build your own DateTimeFormatter
.
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.append(ISO_LOCAL_DATE_TIME) // use the existing formatter for date time
.appendOffset("+HH:MM", "+00:00") // set 'noOffsetText' to desired '+00:00'
.toFormatter();
System.out.println(now.format(dateTimeFormatter)); // 2019-12-20T17:58:06.847274+00:00
这篇关于OffsetDateTime-打印偏移量而不是Z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文