Java 8 DateTimeFormatter解析具有不同重要性的可选小数秒 [英] Java 8 DateTimeFormatter parsing for optional fractional seconds of varying significance

查看:67
本文介绍了Java 8 DateTimeFormatter解析具有不同重要性的可选小数秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 MCVE (作为 TestNG 单元测试):

My MCVE (as a TestNG unit 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 significance using DateTimeFormatter. The relevant part of the Javadoc reads:

分数:以秒为单位输出毫微秒的字段.毫微秒的值具有9位数字,因此,图案字母的计数为1到9.如果小于9,则毫微秒的值将被截断,仅输出最高有效位.

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.

基于我的有限理解,我使用[...]将小数秒标记为可选,并且由于我对各种重要性感兴趣,所以我认为我应该坚持使用SSSSSSSSS.

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.

但是,单元测试无法解析毫秒和毫秒,即第二种和第三种情况.将ResolverStyle更改为 LENIENT 在这里没有帮助,因为它在解析阶段失败,而不是解析失败.

However, the unit test fails at parsing up to milliseconds 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.

我可以知道应该考虑采用哪种方法来解决我的问题吗?我应该使用 DateTimeFormatterBuilder 可以选择指定每个小数位数(9次),还是我的模式有更智能"的方式?

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?

编辑,我最终发现了自己的答案……仍然会在一天之内没有答案,看看是否有其他方法.

edit I 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.

推荐答案

哦,很酷,再进行15分钟的故障排除会得出以下结果:

Oh cool, another 15 minutes of troubleshooting yielded this:

private static final DateTimeFormatter FORMATTER = 
    new DateTimeFormatterBuilder().appendPattern(BASE_PATTERN) // .parseLenient()
        .appendFraction(ChronoField.NANO_OF_SECOND, 0, 9, true).toFormatter();

编辑 parseLenient()可选.

这篇关于Java 8 DateTimeFormatter解析具有不同重要性的可选小数秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆