How to Parse a yyyy-MM-dd Date into a ZonedDateTime in Java

Why ZonedDateTime.parse rejects a plain yyyy-MM-dd string, and how to parse it as a LocalDate and attach a time zone with atStartOfDay, step by step.

A string like 2024-05-10 holds a calendar date and nothing else: no time of day, no offset from UTC and no time-zone region. A ZonedDateTime in Java's java.time API needs all three. Parsing such a string straight into a ZonedDateTime therefore fails unless you supply the missing parts yourself. This guide explains what each step does to your text, how to get a correct result, and how to check it.

What the conversion actually does

The input is a date in the ISO 8601 calendar-date form: four-digit year, hyphen, two-digit month, hyphen, two-digit day. The output is a ZonedDateTime, which is a point on the timeline tied to a named zone such as America/New_York. Moving from one to the other is not just a format change. You are adding two pieces of meaning that the original text did not contain: a time (usually the start of the day) and a zone. Whatever you choose for those determines which instant the result represents, so the choice should be deliberate rather than left to defaults.

Why ZonedDateTime.parse fails on a date-only string

ZonedDateTime.parse(text) with no formatter expects the ISO zoned date-time format, which includes a time and an offset, for example 2024-05-10T00:00-04:00[America/New_York]. A bare date does not match, so it throws a DateTimeParseException. Supplying a formatter with the pattern yyyy-MM-dd does not help on its own either: the text then parses, but the parsed fields contain no time or zone, and the conversion to ZonedDateTime fails with an exception whose message says it is unable to obtain a ZonedDateTime from the parsed data.

Illustrative example of the call that fails:

// Throws DateTimeParseException: no time or zone in the input
ZonedDateTime zdt = ZonedDateTime.parse("2024-05-10");

Recommended approach: parse a LocalDate, then add the zone

Parse the text as what it really is, a LocalDate (a date without time or zone), and then convert it explicitly. LocalDate.parse with no formatter uses DateTimeFormatter.ISO_LOCAL_DATE, which already matches the yyyy-MM-dd shape, so no custom pattern is needed. LocalDate.atStartOfDay(ZoneId) then produces the ZonedDateTime.

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;

String text = "2024-05-10";
ZoneId zone = ZoneId.of("America/New_York");

LocalDate date = LocalDate.parse(text);
ZonedDateTime zdt = date.atStartOfDay(zone);

System.out.println(zdt);

This is an illustrative example, not tested output. Based on the documented behavior, it should print the date at midnight with the zone's offset for that day and the zone ID in brackets, in the form 2024-05-10T00:00-04:00[America/New_York].

atStartOfDay(zone) is preferable to date.atTime(0, 0).atZone(zone) for one reason: according to its documentation, if midnight does not exist on that date in the given zone because of a daylight-saving transition gap, it returns the earliest valid time of that day rather than midnight. Building the time yourself would instead shift the time forward by the gap length, which gives the same instant in many cases but makes the intent less clear.

If you need a custom pattern

When the input is not exactly ISO-shaped, or you want the pattern to be explicit, use DateTimeFormatter.ofPattern and still parse into a LocalDate. The pattern letters are case-sensitive, and swapping the case changes the meaning:

Letter | Meaning | Common mistake
yyyy | Year of era | Usually fine; see the strict-mode note below
uuuu | Proleptic year (can be zero or negative) | Safer choice with ResolverStyle.STRICT
MM | Month of year | mm means minute of hour, not month
dd | Day of month | DD means day of year (1 to 366)
YYYY | Week-based year | Can give the wrong year around January 1 and December 31
import java.time.format.DateTimeFormatter;
import java.time.format.ResolverStyle;

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("uuuu-MM-dd")
        .withResolverStyle(ResolverStyle.STRICT);

ZonedDateTime zdt = LocalDate.parse("2024-05-10", fmt)
        .atStartOfDay(ZoneId.of("America/New_York"));

Strict resolving rejects impossible dates such as 2023-02-30 instead of adjusting them. In strict mode, yyyy (year of era) cannot be resolved without an era field, so pairing yyyy with STRICT causes parse failures; that is why the example uses uuuu. With the default SMART resolver, yyyy-MM-dd works, but out-of-range days are clamped to the last valid day of the month in some cases, which may silently change the meaning of bad input.

Alternative: a single formatter with defaults

If your code must call ZonedDateTime.parse directly, for example because a framework passes it a formatter, you can build a formatter that supplies the missing time and zone. parseDefaulting fills in the hour when the text lacks one, and withZone supplies the zone.

import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

DateTimeFormatter fmt = new DateTimeFormatterBuilder()
        .appendPattern("uuuu-MM-dd")
        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
        .toFormatter()
        .withZone(ZoneId.of("America/New_York"));

ZonedDateTime zdt = ZonedDateTime.parse("2024-05-10", fmt);

This is an illustrative example. It hides the time and zone decisions inside the formatter, so the two-step LocalDate approach is easier to read and review when you control the code.

Choosing the zone

  • Use a named region such as America/Chicago when the date refers to local calendar days in a place; the offset then follows that region's daylight-saving rules.
  • Use ZoneOffset.UTC when the date is meant as a UTC calendar day; the result is always exactly midnight.
  • Avoid ZoneId.systemDefault() unless you truly want the machine's zone. Servers, containers and developer laptops often have different defaults, so the same input can produce different instants on different machines.

Cleaning the input text first

The parser expects exactly ten characters in the form shown. Characters you cannot see will cause a DateTimeParseException even though the string looks correct when printed. Typical culprits are a trailing space or newline (\n), a carriage return (\r) from Windows line endings, a byte order mark (U+FEFF) at the start of a file, and a non-breaking space (U+00A0). Another lookalike problem is a dash that is not the ASCII hyphen-minus (U+002D), such as an en dash (U+2013) pasted from a document.

String cleaned = raw.strip().replace("\uFEFF", "");

String.strip() removes leading and trailing Unicode whitespace, including newlines; it is available from Java 11, while trim() only removes characters up to U+0020 and would leave a non-breaking space in place. Do not replace dashes or other characters inside the date automatically unless you know the source; rejecting malformed input is usually safer than guessing.

Verifying the result

  • Round-trip check: zdt.toLocalDate().toString() should equal the original cleaned input string.
  • Zone check: zdt.getZone() should be the zone you intended, not the system default.
  • Time check: zdt.toLocalTime() should be 00:00, except on dates where that zone skips midnight for daylight saving.
  • Negative test: confirm that clearly invalid input such as 2024-13-01 throws DateTimeParseException rather than producing a date.

Gaps in this guide

No version-specific test results, benchmarks or framework integration details were available for this guide. The behavior described comes from the documented java.time API (introduced in Java 8, with String.strip() added in Java 11). The code samples are illustrative and should be run against your own Java version and inputs before you rely on them.