Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java: DateTimeParseException: Text could not be parsed

The formatter pattern and the input disagree. Check the index in the message, the locale, and whether the text needs a ZonedDateTime instead.

~4 min readJava
#java#datetime#parsing#exceptions
Bold white headline reading PARSE FAILED beside a cyan clock plate rejected by an amber pattern gate.

Introduction & Quick Summary

Parsing a date from a config file, a CSV column or an API response throws:

code
java.time.format.DateTimeParseException: Text '2026-11-28 07:00' could not be parsed at index 10 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2106)

Quick answer: DateTimeFormatter parses with an exact pattern, and the text does not match it at the reported index. index 10 in 2026-11-28 07:00 is the space where the pattern probably expects T. The formatter never guesses the missing pieces; either the pattern must describe the input, or the input must be normalised first.

java
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); LocalDateTime dt = LocalDateTime.parse("2026-11-28 07:00", fmt); // ok LocalDateTime iso = LocalDateTime.parse("2026-11-28T07:00:00"); // default ISO_LOCAL_DATE_TIME

The same exception has three common shapes: a pattern mismatch (index 10), a value out of range (Invalid date 'FEBRUARY 30'), and a missing time zone for a type that needs one. The rest of this guide shows how to read the index, which type to parse into, and how locale changes the result.

Core Mechanics, Signatures & Reproducible Setup

DateTimeFormatter has predefined constants and custom patterns. Parsing produces a TemporalAccessor, and the type you ask for decides which fields must be present.

java
import java.time.*; import java.time.format.*; DateTimeFormatter iso = DateTimeFormatter.ISO_LOCAL_DATE_TIME; // 2026-11-28T07:00:00 DateTimeFormatter spaced = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); ZonedDateTime zoned = ZonedDateTime.parse( "2026-11-28T07:00:00+09:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); Instant instant = Instant.from(DateTimeFormatter.ISO_INSTANT.parse("2026-11-28T07:00:00Z")); LocalDate date = LocalDate.parse("2026-11-28", DateTimeFormatter.ISO_LOCAL_DATE);

Pattern letters are case sensitive and mean different things: yyyy is year-of-era, uuuu is proleptic year, MM is month, mm is minute, HH is hour 0-23 and hh is hour 1-12. A pattern written with mm where MM belongs parses January as minutes and fails at the month, and YYYY (week-based year) produces wrong dates around New Year.

java
// wrong: minutes where month belongs DateTimeFormatter.ofPattern("yyyy-mm-dd") // fails at index 5 // right DateTimeFormatter.ofPattern("uuuu-MM-dd")

A reproducible test for the error text:

java
public class Parse { public static void main(String[] args) { var f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); try { LocalDateTime.parse("2026-11-28 07:00:00", f); } catch (DateTimeParseException e) { System.out.println(e.getMessage()); System.out.println("parsed up to: " + e.getParsedString()); System.out.println("error index: " + e.getErrorIndex()); } } }

getErrorIndex() and getParsedString() are the accessors worth knowing: the first points at the exact character, the second returns the input.

Key Comparison Table

Target typeRequiresExample inputFails when
LocalDatedate fields only2026-11-28time zone text present
LocalDateTimedate and time2026-11-28T07:00offset present with strict mode
OffsetDateTimedate, time, offset2026-11-28T07:00+09:00offset missing
ZonedDateTimedate, time, zone or offset2026-11-28T07:00+09:00[Asia/Seoul]zone missing
Instantinstant with offset or Z2026-11-28T07:00:00Zlocal time only
SymptomMeaningFix
could not be parsed at index 10character 10 does not matchalign pattern with input
could not be parsed at index 0input empty or wrong typevalidate before parsing
Invalid date 'FEBRUARY 30'value out of rangeResolverStyle or validation
Unable to obtain LocalDateTimeparsed object lacks fieldsparse to LocalDate or add time
could not be parsed, unparsed text found at index 16trailing charactersuse parse(CharSequence, ParsePosition) or trim
Formatter setupBehaviourWhen to use
ofPattern("yyyy-MM-dd")uses default localenot recommended
ofPattern("yyyy-MM-dd", Locale.ROOT)locale independentparsing machine data
ISO_LOCAL_DATEfixed ISO formatexternal APIs
ofPattern(..., Locale.KOREA)localised month nameshuman input
parseBestpicks the first matching typemixed offset input

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Read the index, not the whole message

The index is measured in characters of the input, so it pinpoints the disagreement:

java
String[] inputs = {"2026-11-28 07:00", "2026-11-28T07:00", "28/11/2026 07:00"}; DateTimeFormatter f = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm"); for (String s : inputs) { try { System.out.println(LocalDateTime.parse(s, f)); } catch (DateTimeParseException e) { System.out.println(s + " -> index " + e.getErrorIndex()); } } // 2026-11-28 07:00 -> ok // 2026-11-28T07:00 -> index 10 // 28/11/2026 07:00 -> index 0

An index of 0 means the first character is already wrong (day-first input against a year-first pattern); an index at a separator means the separator differs.

2. Accept several shapes with parseBest

Real input often mixes offset and local timestamps. parseBest returns the first type the text can satisfy, which turns a chain of try/catch blocks into one call:

java
DateTimeFormatter flexible = DateTimeFormatter.ofPattern( "[uuuu-MM-dd['T'][ ]HH:mm[:ss][XXX]]", Locale.ROOT); TemporalAccessor parsed = flexible.parseBest( "2026-11-28 07:00", ZonedDateTime::from, OffsetDateTime::from, LocalDateTime::from, LocalDate::from); System.out.println(parsed.getClass().getSimpleName()); // LocalDateTime

Square brackets mark optional sections, the supported way to model "with or without seconds and offset" without several formatters. The order of the method references matters: most specific type first.

3. Locale decides month names and week rules

Month names, week start and the meaning of Y depend on locale; parsing English month names with a Korean default fails at the month:

java
DateTimeFormatter en = DateTimeFormatter.ofPattern("d MMM uuuu", Locale.ENGLISH); LocalDate d = LocalDate.parse("28 Nov 2026", en); // ok DateTimeFormatter wrong = DateTimeFormatter.ofPattern("d MMM uuuu"); // default locale LocalDate.parse("28 Nov 2026", wrong); // DateTimeParseException

Use Locale.ROOT or an explicit locale for machine data, and the JVM default only for text a human typed.

4. Strict versus smart resolution

DateTimeFormatter defaults to ResolverStyle.SMART, which resolves 2026-02-30 to 2026-02-28 in some paths and throws in others depending on the fields requested. Validation code should use STRICT and uuuu rather than yyyy:

java
DateTimeFormatter strict = DateTimeFormatter .ofPattern("uuuu-MM-dd") .withResolverStyle(ResolverStyle.STRICT); LocalDate.parse("2026-02-30", strict); // DateTimeParseException: Invalid date 'FEBRUARY 30' LocalDate.parse("2026-11-28", strict); // ok

Using yyyy with STRICT throws Unable to obtain LocalDate from TemporalAccessor, a confusing message for a pattern that looked correct.

5. Time zones and DST gaps

A LocalDateTime has no zone, so it cannot represent a DST transition; converting with atZone may throw or shift an hour depending on the resolver.

java
ZoneId seoul = ZoneId.of("Asia/Seoul"); ZonedDateTime z = LocalDateTime.parse("2026-11-28T07:00").atZone(seoul); // gaps and overlaps LocalDateTime gap = LocalDateTime.parse("2026-03-29T02:30"); // Europe: does not exist gap.atZone(ZoneId.of("Europe/Berlin")); // shifted by default, throws with strict gap handling

When the source is an external system, parse an OffsetDateTime or Instant and convert to the display zone, rather than parsing a local time and guessing the zone later. For converting a parsed value to epoch seconds, the timestamp converter is quicker than a scratch Java file.

6. Parsing inside a loop

A DateTimeFormatter is immutable and thread-safe, so create it once as a static final field. Building it inside a hot loop costs time, and catching DateTimeParseException per row is expensive because the exception fills a stack trace:

java
private static final DateTimeFormatter CSV_TIME = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ROOT); static Optional<LocalDateTime> parseRow(String value) { try { return Optional.of(LocalDateTime.parse(value, CSV_TIME)); } catch (DateTimeParseException e) { return Optional.empty(); // caller counts bad rows } }

Pre-validating with a regular expression avoids most exceptions; test the pattern with the regex tester before wiring it into the parser.

Common Compilation/Runtime Mistakes & Version Gotchas

  • yyyy instead of uuuu. With STRICT resolution, year-of-era without an era cannot be resolved; uuuu is the predictable choice.
  • mm for months or hh for 24-hour time. Both produce parse failures far from the actual mistake in the pattern.
  • Catching DateTimeParseException as Exception. The specific type carries getErrorIndex(), the only cheap clue in the log.
  • Assuming SimpleDateFormat behaves the same. It is lenient by default, not thread-safe, and uses different pattern letters.
  • Version notes. Java 8 introduced java.time; DateTimeFormatter has been thread-safe since then, and ResolverStyle.LENIENT still resolves impossible dates instead of throwing, so validation logic must not rely on the default. Java 21 adds no changes to parsing behaviour.
  • Ignoring the offset in the input. LocalDateTime.parse on a string with +09:00 fails at the offset index; use OffsetDateTime.

Official documentation: DateTimeFormatter and DateTimeParseException.

Official references

primary documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.