You are currently viewing How to Fix DateTimeParseException In Java

How to Fix DateTimeParseException In Java

1. Introduction

When dealing with date/time in Java, DateTimeParseException is an exception you’ll probably encounter. In this article, you’ll learn how to fix this exception. You’ll also learn some best practices for avoiding the DateTimeParseException.

2. What Java Says about DateTimeParseException

According to the Javadoc, a DateTimeParseException is thrown to indicate problems when parsing datetime objects.

3. How to Reproduce

3.1. Using the LocalDate Class

Let’s consider the following code which attempts to create a LocalDate with an invalid String value:

        String invalidDateFormat = "01/05/2024";// Non ISO-8601 date format
        LocalDate date = LocalDate.parse(invalidDateFormat);// DateTimeParseException

Running the above code snippet will print the following to the console:

Exception in thread "main" java.time.format.DateTimeParseException: Text '01/05/2024' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalDate.parse(LocalDate.java:430)
    at java.base/java.time.LocalDate.parse(LocalDate.java:415)
    at Scratch.main(scratch.java:14)

By default, Java uses the standard ISO datetime format.

        String validDateFormat = "2024-05-01";// ISO-8601 date format
        LocalDate date = LocalDate.parse(validDateFormat);// No DateTimeParseException

3.2. Using the LocalTime Class

The code below is attempting to parse a LocalTime with an invalid hour value:

        String invalidTimeFormat = "04:00 PM";// Non ISO-8601 date format
        LocalTime date = LocalTime.parse(invalidTimeFormat);// DateTimeParseException

Output:

Exception in thread "main" java.time.format.DateTimeParseException: Text '04:00 PM' could not be parsed at index 0
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalDate.parse(LocalDate.java:430)
    at java.base/java.time.LocalDate.parse(LocalDate.java:415)
    at Scratch.main(scratch.java:8)

Like LocalDate, LocaleTime uses the standard ISO format, a 24-hour time format.

        String validTimeFormat = "16:00";// ISO-8601 date format
        LocalTime date = LocalTime.parse(validTimeFormat);// No DateTimeParseException

3.3. Using the DateTimeFormatter Class

A common source of DateTimeParseException is when you create a DateTimeFormatter without explicitly passing a Locale object.

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a");
        LocalDateTime date = LocalDateTime.parse("2024-05-29 03:05:00 PM",formatter);//DateTimeFormatter if run on a system with the India Locale

If you run the above code on a system with a Locale that uses “am/pm” symbols instead of “AM/PM”, a DateTimeParseException is thrown. This will happen for example if you run the code in India.

Exception in thread "main" java.time.format.DateTimeParseException: Text '2024-05-29 03:05:00 PM' could not be parsed at index 20
    at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052)
    at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
    at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494)
    at Scratch.main(scratch.java:11)

You must explicitly pass a Locale to the DateTimeFormatter to fix this error.

        Locale locale = new Locale("en");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", locale);
        LocalDateTime date = LocalDateTime.parse("2024-05-29 03:05:00 PM",formatter);//No DateTimeFormatter

4. Best Practices to Avoid DateTimeParseException

4.1. Always pass a Locale when constructing a DateTimeFormatter

You should always provide an appropriate Locale object to the DateTimeFormatter during its creation. This will prevent unexpected behavior if your code is run on a different system.

        Locale locale = new Locale("in");//India
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a", locale);
        LocalDateTime date = LocalDateTime.parse("2024-05-29 03:05:00 pm",formatter);//No DateTimeFormatter

4.2. Use Suitable Data Types

Always make sure you choose the proper data type according to your needs:

  • Use LocalDate for date-only values: “2024-05-25”
  • Use LocalTime for time-only values: “15:30”
  • Use LocalDateTime for combined date and time values: “2024-05-25T15:30”
  • Use ZonedDateTime or OffsetDateTime for date and time values with time zone or offset: “2024-05-25T15:30:15.962090600+02:00[Europe/Paris]”

5. Conclusion

In this brief tutorial, you learned about the DateTimeParseException and how to fix it.

Noel Kamphoa

Experienced software engineer with expertise in Telecom, Payroll, and Banking. Now Senior Software Engineer at Societe Generale Paris.