Java LocalDateTime.parse具有毫秒精度,但可选的微秒精度 [英] Java LocalDateTime.parse with millisecond precision but optional microsecond precision

查看:1595
本文介绍了Java LocalDateTime.parse具有毫秒精度,但可选的微秒精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以创建LocalDateTime模式,该模式将解析具有至少毫秒精度但可选的微秒精度的日期/时间,即在接下来的毫秒日期/时间中解析正常,但第二个以毫秒为单位的日期/时间失败.我认为模式[["]中的可选指示器将允许这项工作:

Is there a way to create a LocalDateTime pattern that will parse a date/time that has at least millisecond precision but optional microsecond precision i.e. in the following the millisecond date/time is parsed ok but the second one in microseconds fails. I thought the optional indicators in the pattern "[" "]" would allow this work:

DateTimeFormatter DATE_TIME_FORMATTER = 
    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS[SSS]");
System.out.println(LocalDateTime.parse("2019-02-14 11:04:52.784", DATE_TIME_FORMATTER));     
System.out.println(LocalDateTime.parse("2019-02-14 11:04:52.784108", DATE_TIME_FORMATTER));

推荐答案

提示编号. 1

    String withMillis = "2019-02-14 11:04:52.784";
    String withMicros = "2019-02-14 11:04:52.784108";

    System.out.println(LocalDateTime.parse(withMillis.replace(' ', 'T')));
    System.out.println(LocalDateTime.parse(withMicros.replace(' ', 'T')));

2019-02-14T11:04:52.784
2019-02-14T11:04:52.784108

当我们用T替换字符串中间的空格时,该字符串符合ISO 8601,LocalDateTime和java.time其他类解析(并打印)的标准格式默认情况下,即没有任何显式格式化程序.因此,这是一个简单的解决方案.

When we replace the space in the middle of your string with a T, the string conforms to ISO 8601, the standard format that LocalDateTime and the other classes of java.time parse (and also print) as their default, that is, without any explicit formatter. So this is an easy solution.

可以使您尝试的工作正常.只有您不能拆分SSSSSS的序列,该序列的中间有一个方括号.

Something like what you tried can be made to work. Only you cannot split a sequence of SSSSSS with a square bracket in the middle.

static final DateTimeFormatter DATE_TIME_FORMATTER = 
        DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.[SSSSSS][SSS]");

然后:

    System.out.println(LocalDateTime.parse(withMillis, DATE_TIME_FORMATTER));     
    System.out.println(LocalDateTime.parse(withMicros, DATE_TIME_FORMATTER));

我可以选择指定6个小数,然后指定3个小数.我们需要那个命令.如果我们放入[SSS][SSSSSS]并尝试解析6个小数,则格式化程序将首先解析3个小数,然后引发异常,因为它无法使用SSSSSS解析其余3个小数.这有点麻烦,因为它也可以接受一个不带小数点的小数点,并且如果我们给它9个小数点的话,可能会发出非常混乱的错误消息(甚至可能给出错误的结果).

I specify optionally 6 decimals and then optionally 3 decimals. We need that order. If we put [SSS][SSSSSS] and try to parse 6 decimals, the formatter will first parse 3 and then throw an exception because it cannot parse the remaining 3 with SSSSSS. It’s a bit of a hack since it will also accept a decimal point with no decimals at all, and will probably issue a very confusing error message (or possibly even give an incorrect result) if we give it 9 decimals.

static final DateTimeFormatter DATE_TIME_FORMATTER = 
        new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE)
                .appendLiteral(' ')
                .appendPattern("HH:mm:ss")
                .appendFraction(ChronoField.NANO_OF_SECOND, 1, 9, true)
                .toFormatter();

在这里,我指定了小数点后最少1个小数,最多9个小数.如果愿意,可以指定3和6.当然,它也会接受4或5.

Here I have specified a minimum of 1 and a maximum of 9 decimals after the decimal point. You can specify 3 and 6 if you prefer. It will of course accept 4 or 5 too.

链接: 维基百科文章:ISO 8601

这篇关于Java LocalDateTime.parse具有毫秒精度,但可选的微秒精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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