Java 8 DateTimeFormatter 解析具有不同重要性的可选小数秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30090710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance
提问by h.j.k.
My MCVE(as a TestNGunit test):
public class MyDateTimeFormatterTest {
private static final String BASE_PATTERN = "yyyy/MM/dd HH:mm:ss";
private static final DateTimeFormatter FORMATTER =
DateTimeFormatter.ofPattern(BASE_PATTERN + "[.SSSSSSSSS]");
private static final LocalDateTime TEST_INPUT =
LocalDateTime.of(2015, 5, 4, 12, 34, 56, 123456789);
@DataProvider(name = "test-cases")
public Iterator<Object[]> getTestCases() {
return Arrays.asList(testFor("", ChronoUnit.SECONDS),
testFor(".SSS", ChronoUnit.MILLIS),
testFor(".SSSSSS", ChronoUnit.MICROS),
testFor(".SSSSSSSSS", ChronoUnit.NANOS)).iterator();
}
@Test(dataProvider = "test-cases")
public void testWithDefaultResolution(String input, LocalDateTime output) {
assertThat(FORMATTER.parse(input, LocalDateTime::from), equalTo(output));
}
private Object[] testFor(String patternSuffix, TemporalUnit truncatedTo) {
return new Object[] { DateTimeFormatter.ofPattern(BASE_PATTERN + patternSuffix)
.format(TEST_INPUT), TEST_INPUT.truncatedTo(truncatedTo) };
}
}
I am trying to test the parsing of a date-time String
with optional fractional seconds of varying significanceusing DateTimeFormatter
. The relevant part of the Javadoc reads:
我想测试的日期时间解析String
与不同意义的可选的小数秒使用DateTimeFormatter
。Javadoc 的相关部分内容如下:
Fraction: Outputs the nano-of-second field as a fraction-of-second. The nano-of-second value has nine digits, thus the count of pattern letters is from 1 to 9. If it is less than 9, then the nano-of-second value is truncated, with only the most significant digits being output.
Fraction:输出纳秒字段作为秒的分数。纳秒值有九位数字,因此模式字母的计数是从 1 到 9。如果它小于 9,那么纳秒值将被截断,只输出最重要的数字。
Based on my limited understanding, I used [...]
to mark the fractional seconds as optional, and since I'm interested in varying significance, I thought I should stick to SSSSSSSSS
.
基于我有限的理解,我曾经[...]
将小数秒标记为可选,并且由于我对不同的重要性感兴趣,我认为我应该坚持使用SSSSSSSSS
.
However, the unit test fails at parsing up tomilliseconds and microseconds, i.e. the second and third cases. Changing the ResolverStyle
to LENIENT
does not help here as it fails at the parsing stage, not the resolution.
然而,单元测试失败在解析到毫秒和微秒,即,第二和第三种情况。更改ResolverStyle
toLENIENT
在这里没有帮助,因为它在解析阶段失败,而不是解析阶段。
May I know which approaches should I consider to resolve my problem? Should I be using DateTimeFormatterBuilder
to optionally specify each fractional digit (9 times), or is there a 'smarter' way with my pattern?
我可以知道我应该考虑哪些方法来解决我的问题吗?我应该使用DateTimeFormatterBuilder
来选择指定每个小数位数(9 次),还是我的模式有“更聪明”的方法?
editI found my own answer in the end... will still leave this as unanswered for a day and see if there are other approaches or not.
编辑我最后找到了我自己的答案......仍然会在一天内没有回答这个问题,看看是否还有其他方法。
回答by h.j.k.
Oh cool, another 15 minutes of troubleshooting yielded this:
哦,太酷了,又进行了 15 分钟的故障排除,结果如下:
private static final DateTimeFormatter FORMATTER =
new DateTimeFormatterBuilder().appendPattern(BASE_PATTERN) // .parseLenient()
.appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true).toFormatter();
editparseLenient()
is optional.
编辑parseLenient()
是可选的。