The Java 8 DateTimeParseException
is thrown when parsing a text to the Date object using the java.time
API.
The error message "DateTimeParseException:Text could not be parsed at index
” occurs because of any issue with the input string at the specified position while converting the date-time string to date object.
In this tutorial, you will learn about different reason for DateTimeParseException to occur while parsing date-time string in Java.
Incorrect Date-Time Input String
The Java 8 DateTimeParseException is thrown with the text being parsed and the error index while parsing the date-time string.
When the input string and the date-time formatter pattern do not match, which is the likely to cause DateTimeParseException. To resolve this problem, you must pass an input that adheres to the expected format.
To understand the DateTimeParseException, it occurs when an incorrect input string(2023-02-17T07:35:51.5325
) is passed that does not match the pattern(DateTimeFormatter.ISO_OFFSET_DATE_TIME
).
The DateTimeFormatter.ISO_OFFSET_DATE_TIME
expects a time zone value, and the input string is not given with a time zone value(Z) and results in DateTimeParseException.
DateTimeParseException can be avoided using proper input string matching the pattern.
Code
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
//main
String dateString = "2023-02-17T07:35:51.5325";
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
System.out.println(dateTime);
Output
Exception in thread "main" java.time.format.DateTimeParseException: Text '2023-02-17T07:35:51.5325' could not be parsed at index 24
To resolve the issue, you need to include the time zone value to the input string(2023-02-17T07:35:51.5325Z
) as shown below,
Code
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
//main
String dateString = "2023-02-17T07:35:51.5325Z";
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
System.out.println(dateTime);
Output
2023-02-17T07:35:51.532500
Check this tutorial to parse a string to LocalDateTIme value, How to parse/format dates with Java 8 LocalDateTime.
Incorrect Time Symbol in DateTimeFormatter
The DateTimeFormatter helps in parsing a string-to-date object. If the pattern used to parse is not configured to match the input string, it results in DateTimeParseException.
Use proper formatting pattern matching the input string expectation to avoid DateTimeParseException.
The code below throws an exception due to an incorrect pattern that does not match the input string.
Code
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
//main
String dateString = "20220218 08:09:10";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd H:mm:S", Locale.US);
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
System.out.println(dateTime);
Output
Exception in thread "main" java.time.format.DateTimeParseException: Text '20220218 08:09:10' could not be parsed; unparsed text found at index 16
The above DateTimeParseException explains that the formatter cannot parse the input string("20220218 08:09:10"
) at index position 16 because the formatter contains an incorrect symbol(yyyyMMdd H:mm:S
).
The formatter pattern is corrected with a valid pattern(yyyyMMdd H:mm:ss
) to accept the above input string to resolve the issue.
Code
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
//main
String dateString = "20220218 08:09:10";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd H:mm:ss", Locale.US);
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
System.out.println(dateTime);
Output
2022-02-18T08:09:10
Incorrect Date Symbol in DateTimeFormatter
The examples below help explain another possibility to get DateTimeParseException when the pattern contains incorrect Date representation.
The DateTimeFormatter pattern(yyyyMMDD
) is configured to represent the day of the year(D)
instead of the day of the month(d)
. This is an incorrect representation for the given input string and throws DateTimeParseException as shown.
Code
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
//main
String dateString= "20220218";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMDD", Locale.US);
LocalDate dateTime = LocalDate.parse(dateString, formatter);
System.out.println(dateTime);
Output
Exception in thread "main" java.time.format.DateTimeParseException: Text '20220218' could not be parsed at index 0
So you need to configure the proper pattern(yyyyMMdd
) to parse the given input string to Date and avoid the DateTimeParseException as below,
Code
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
//main
String dateString= "20220218";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd", Locale.US);
LocalDate dateTime = LocalDate.parse(dateString, formatter);
System.out.println(dateTime);
Output
2022-02-18