gpt4 book ai didi

java - 如何在 Java 8 中解析不包含标点符号的 ISO-8601 格式的字符串?

转载 作者:行者123 更新时间:2023-12-02 09:54:50 28 4
gpt4 key购买 nike

如何将以下字符串解析为 LocalDateTime-对象?

20200203092315000000

我总是遇到以下异常,但我不明白:

java.time.format.DateTimeParseException: Text '20200203092315000000' could not be parsed at index 0

at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDateTime.parse(LocalDateTime.java:492)
at de.x.struct.type.LocalDateTimeStructField.setBytesValue(LocalDateTimeStructField.java:44)
at de.x.struct.Struct.bytesToStruct(Struct.java:110)
at de.x.struct.StructTest.testStringToStruct(StructTest.java:60)

我的应用程序代码如下所示:

LocalDateTime ldt = LocalDateTime.parse("20200203092315000000", DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSSSSS"));

最佳答案

看起来像是一个已知问题...

bug_id=JDK-8031085 bug_id=JDK-8138676

Workaround:

DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("yyyyMMddHHmmss").appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter()

CUSTOMER SUBMITTED WORKAROUND : use the following format (mind the '.'): "yyyyMMddHHmmss.SSS"

LocalDateTime.parse("20150910121314987", DateTimeFormatter.ofPattern("yyyyMMddHHmmss.SSS"))

or alternatively use jodatime library

关于java - 如何在 Java 8 中解析不包含标点符号的 ISO-8601 格式的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59576976/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com