LocalDate.parse导致“DateTimeParseException…无法解析:null”

huangapple go评论82阅读模式
英文:

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:ssX");
        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。但是我的堆栈行号与问题中的行号不匹配。欢迎任何关于可能出错的指示。

附注:

$ 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)
英文:

I am attempting to parse a date time stamp and extract only the date. Like so:

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));
    }
}

But this results in the following Exception:

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

This seems related to this issue: https://bugs.openjdk.java.net/browse/JDK-8226704. But the line numbers in my stack do not match up with those in the issue. Appreciate any pointers on what could be going wrong.

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)

答案1

得分: 1

在你的时间戳字符串末尾的那个 'Z'?你使用模式字符串中的 X 可以得到它。而不是 z。在格式字符串中,小写的 z 表示 '时区名称',并匹配诸如 PSTPacific Standard Time 之类的内容。

X 是一个缩写的区域偏移。类似于 -08Z

英文:

The 'Z' at the end there in your timestamp string? You get that with X in your pattern string. Not z. lower-case z in the format string means 'time zone name' and matches something like PST or Pacific Standard Time.

X is a shortened zone offset. something like -08 or Z.

huangapple
  • 本文由 发表于 2020年8月27日 23:12:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63619042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定