[英]LocalDate.parse results in “DateTimeParseException…could not be parsed: null”
我正在嘗試解析日期時間戳並僅提取日期。 像這樣:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class FormatterMain {
public static void main(String[] args) {
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");
System.out.println(LocalDate.parse("2020-10-14T10:00:00Z", formatter));
}
}
但這會導致以下異常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-10-14T10:00:00Z' could not be parsed: null
at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2017)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1952)
at java.base/java.time.LocalDate.parse(LocalDate.java:428)
at FormatterMain.main(FormatterMain.java:8)
Caused by: java.lang.NullPointerException
at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.prefixLength(DateTimeFormatterBuilder.java:4527)
at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add0(DateTimeFormatterBuilder.java:4396)
at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add(DateTimeFormatterBuilder.java:4391)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.getTree(DateTimeFormatterBuilder.java:4138)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneIdPrinterParser.parse(DateTimeFormatterBuilder.java:4249)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.parse(DateTimeFormatterBuilder.java:2370)
at java.base/java.time.format.DateTimeFormatter.parseUnresolved0(DateTimeFormatter.java:2107)
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2036)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
... 2 more
這似乎與這個問題有關: https : //bugs.openjdk.java.net/browse/JDK-8226704 。 但是我堆棧中的行號與問題中的行號不匹配。 感謝有關可能出現問題的任何指示。
PS:
$ java -version
openjdk version "11.0.7" 2020-04-14 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.7+10-LTS, mixed mode, sharing)
時間戳字符串中末尾的“Z”? 您可以通過模式字符串中的X
獲得它。 不是z
。 格式字符串中的小寫z
表示“時區名稱”並匹配PST
或Pacific Standard Time
。
X
是縮短的區域偏移量。 像-08
或Z
類的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.