DateTimeFormatter.ISO_OFFSET_DATE_TIME的等效格式字符串是什么? [英] What is the equivalent format string of DateTimeFormatter.ISO_OFFSET_DATE_TIME?
问题描述
我们是否知道是否存在与 DateTimeFormatter.ISO_OFFSET_DATE_TIME
输出相同结果的等效格式字符串?
Do we know if there is an equivalent format string that outputs the same result as DateTimeFormatter.ISO_OFFSET_DATE_TIME
?
ie
ZonedDateTime dateTime = ZonedDateTime.now();
System.out.println(dateTime.format(DateTimeFormatter.ofPattern(pattern)));
System.out.println(dateTime.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME));
会输出相同的
推荐答案
不,这是不可能的
根据要求:是的,我们知道没有等效的格式模式字符串 DateTimeFormatter.ISO_OFFSET_DATE_TIME
。
DateTimeFormatter.ISO_OFFSET_DATE_TIME
省略秒和/或纳米如果他们是零,则为秒。如果nanos为非零将根据需要输出多个数字。没有模式字母或模式字母组合可以提供相同的行为。
DateTimeFormatter.ISO_OFFSET_DATE_TIME
omits the seconds and/or nano of second if they are zero. If the nanos are non-zero "As many digits will be output as required." There is no pattern letter or combination of pattern letters that will give you the same behaviour.
深入 DateTimeFormatter
, ISO_OFFSET_DATE_TIME
使用 ISO_LOCAL_TIME
,其中反过来以这种方式定义:
Deep inside DateTimeFormatter
, ISO_OFFSET_DATE_TIME
uses an ISO_LOCAL_TIME
, which in turn is defined in this way:
ISO_LOCAL_TIME = new DateTimeFormatterBuilder()
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.optionalStart()
.appendFraction(NANO_OF_SECOND, 0, 9, true)
.toFormatter(ResolverStyle.STRICT, null);
获取动态行为的方法是:使用 DateTimeFormatterBuilder
及其 optionalStart
和 appendFraction
方法。
Which is the way to obtain the dynamic behaviour: using a DateTimeFormatterBuilder
and its optionalStart
and appendFraction
methods.
另外,您不希望完全复制 ISO_OFFSET_DATE_TIME
的行为。您将需要使用内置格式化程序。
As an aside, you don’t want to copy the behaviour of ISO_OFFSET_DATE_TIME
exactly. You will want to use the built-in formatter.
这篇关于DateTimeFormatter.ISO_OFFSET_DATE_TIME的等效格式字符串是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!