Skip to content

LocalDate.parse results in “DateTimeParseException…could not be parsed: null”

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 =
        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(
    at java.base/java.time.format.DateTimeFormatter.parse(
    at java.base/java.time.LocalDate.parse(
    at FormatterMain.main(
Caused by: java.lang.NullPointerException
    at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.prefixLength(
    at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add0(
    at java.base/java.time.format.DateTimeFormatterBuilder$PrefixTree.add(
    at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser.getTree(
    at java.base/java.time.format.DateTimeFormatterBuilder$ZoneIdPrinterParser.parse(
    at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.parse(
    at java.base/java.time.format.DateTimeFormatter.parseUnresolved0(
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(
    at java.base/java.time.format.DateTimeFormatter.parse(
    ... 2 more

This seems related to this issue: 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.


$ 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)


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.