HOWTO · Java
How to Fix the java.text.ParseException: Unparseable Date Error in Java
This tutorial demonstrates the java.text.ParseException: Unparseable date error in Java.
On this page
This guide explains how to diagnose java.text.ParseException: Unparseable date when a Java date string does not match its parsing pattern. Compare the input from left to right with the pattern, then account for localized text and time zones before formatting the parsed value.
Fix the java.text.ParseException: Unparseable date Error in Java
SimpleDateFormat parses text according to pattern letters; it does not guess the intended date layout. A parse failure usually means the pattern expects different fields or text than the input supplies. For example, Tue Aug 16 12:30:10 IST 2022 begins with a weekday, then an English month name, a day, a time, a zone abbreviation, and a year. A pattern such as MMM d, yyyy HH:mm:ss expects different punctuation and omits the weekday and zone, so it cannot consume that string as written.
Inspect the complete input, including separators and spaces. Check the weekday, month text, day, time fields, zone, and year. A ParseException identifies the position where parsing stopped; compare the text at that point with the next field required by the pattern. Do not treat a time-zone difference as the cause of this exception by itself: a zone generally affects the instant represented or how it is displayed.
Match the Date Pattern to the Input String
Pattern letters are case-sensitive. MM is the month, while mm is minutes. Use yyyy for the calendar year; uppercase YYYY means week-based year and can produce surprising results near a year boundary. The count and order of fields must also fit the input. For the supplied date text, the pattern needs a weekday (EEE), abbreviated month (MMM), two-digit day (dd), 24-hour time (HH:mm:ss), zone (z), and year (yyyy).
A useful diagnosis is to write the input and pattern side by side and match each token in sequence. If the text contains numeric month and day fields, use a numeric pattern with the same order and separators. If it contains a month name, use a textual month pattern. Do not “fix” the pattern by changing a field’s capitalization without checking what that letter means.
Set the Parsing Locale and Time Zone Deliberately
Textual month and weekday names are locale-sensitive. If input contains English names, pass Locale.ENGLISH rather than relying on the machine’s default locale. The default can differ between developer machines and servers. Use the locale that produced the text, and keep it explicit so the parser expects the same month and weekday vocabulary.
A date string that includes a zone carries information needed to interpret its time. Zone abbreviations such as IST can be ambiguous across regions and data sources. Prefer an unambiguous numeric offset or a region-based zone identifier in formats you control. When parsing existing text, confirm what the abbreviation means to its producer. Set a time zone explicitly when formatting so the displayed clock time does not silently depend on the host machine. This affects interpretation or display; it does not repair mismatched date fields.
Run a Complete Parsing and Output-Formatting Example
With the mismatched pattern MMM d, yyyy HH:mm:ss, parsing the supplied input with new SimpleDateFormat("MMM d, yyyy HH:mm:ss", Locale.ENGLISH).parse(input) fails because the pattern expects a month name at the start, while the input starts with a weekday. The resulting exception is java.text.ParseException: Unparseable date: "Tue Aug 16 12:30:10 IST 2022". The corrected pattern below accounts for the weekday, month, day, time, zone, and year. It uses an English locale and formats the parsed instant in UTC. To make the displayed result deterministic, this example uses UTC in its input instead of the ambiguous IST abbreviation shown above. In application code, prefer an unambiguous zone representation when you control the input format.
Run this example:
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
public class ParseDateExample {
public static void main(String[] args) {
String input = "Tue Aug 16 10:30:10 UTC 2022";
SimpleDateFormat parser = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
ParsePosition position = new ParsePosition(0);
Date parsed = parser.parse(input, position);
if (parsed == null || position.getIndex() != input.length()) {
System.out.println("Input did not match the pattern.");
return;
}
SimpleDateFormat display = new SimpleDateFormat(
"EEE MMM d, yyyy HH:mm:ss 'UTC'", Locale.ENGLISH);
display.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(display.format(parsed));
}
}
Tue Aug 16, 2022 10:30:10 UTC
The parser reads the source text, while the second formatter selects a readable output layout and a deliberate display zone.