[Solved] java.time.format.DateTimeParseException: Text could not be parsed at index

In this post, I will be sharing how to fix the java.time.format.DateTimeParseException: Text could not be parsed at index in Java with examples. According to Oracle docs, it is thrown when an error occurs during parsing. DateTimeParseException includes the text being parsed and the error index. It is an unchecked exception since it extends RuntimeException.

Read Also: 2 ways to Convert String to LocalDateTime in Java

DateTimeParseException class is intended for use in a single thread. Let's dive deep into the topic:

[Fixed] java.time.format.DateTimeParseException: Text could not be parsed at index

As always, first, we will produce the java.time.format.DateTimeParseException: Text could not be parsed at index exception before moving on to the solution.

1. Producing the exception while converting String to LocalDate


java.time.format.DateTimeParseException: Text could not be parsed at index can be produced due to a  mismatch between the pattern of DateTimeFormatter and given date string format as shown below in the example.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class DateTimeParseExceptionExample { public static void main(String args[]) { // Given String String dateStr = "20221218"; // Specifying the date format using DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMDD", Locale.US);
// Converting given date string to LocalDate LocalDate dateTime = LocalDate.parse(dateStr, dtf); // Print LocalDate object System.out.println(dateTime); } }


Output:
Exception in thread "main" java.time.format.DateTimeParseException: Text '20221218' could not be parsed at index 0


Solution


In the above example, we are getting java.time.format.DateTimeParseException because of the incorrect pattern passed in the DateTimeFormatter.ofPattern() method i.e we should pass yyyyMMdd instead of yyyyMMDD. Also yyyyMMd, yyyymmdd, YYYYMMDD, etc. patterns will result in DateTimeParseException in the above example.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class DateTimeParseExceptionExample { public static void main(String args[]) { // Given String String dateStr = "20221218"; // Specifying the date format using DateTimeFormatter
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd", Locale.US);
// Converting given date string to LocalDate LocalDate dateTime = LocalDate.parse(dateStr, dtf); // Print LocalDate object System.out.println(dateTime); } }


Output:
2022-12-18


2. Producing the exception while converting String to LocalDateTime


String to LocalDateTime conversion also produces the DateTimeParseException due to a  mismatch between the pattern of DateTimeFormatter and the given date string format as shown below in the example.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateTimeParseExceptionExample2 {
    public static void main(String args[]) {
      // Given String    
String dateStr = "18/DEC/2022 08:45:25";
// Specifying the date format using DateTimeFormatter DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MMM/yyyy HH:mm:ss", Locale.US); // Converting given date string to LocalDateTime LocalDateTime dateTime = LocalDateTime.parse(dateStr, dtf); // Print LocalDateTime object System.out.println(dateTime); } }


Output:
Exception in thread "main" java.time.format.DateTimeParseException: Text '18/DEC/2022 08:45:25' could not be parsed at index 3


Solution


We can easily get rid of the DateTimeParseException by changing the dateStr from "18/DEC/2022 08:45:25" to "18/Dec/2022 08:45:25". In simple words, Dec is valid since we are expecting MMM format. DEC, December, 12 are invalid for the above example.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class DateTimeParseExceptionExample2 {
    public static void main(String args[]) {
      // Given String    
String dateStr = "18/Dec/2022 08:45:25";
// Specifying the date format using DateTimeFormatter DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MMM/yyyy HH:mm:ss", Locale.US); // Converting given date string to LocalDateTime LocalDateTime dateTime = LocalDateTime.parse(dateStr, dtf); // Print LocalDateTime object System.out.println(dateTime); } }


Output:
2022-12-18T08:45:25


That's all for today. Please mention in the comments if you have any questions related to how to fix java.time.format.DateTimeParseException: Text could not be parsed at index in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry