Illegal pattern character 'T' when parsing a date string to java.util.Date
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, working with dates is a common requirement that often involves converting strings into java.util.Date objects. This is typically achieved using the SimpleDateFormat class, which formats and parses dates in a locale-sensitive manner. However, during this conversion process, one may encounter an error related to illegal pattern characters, such as 'T'. Let's delve deeper into the problem of the Illegal pattern character 'T', understand why it occurs, and explore ways to resolve it.
Understanding the Error
What is an Illegal Pattern Character?
When you use SimpleDateFormat to parse a date string, a format pattern is specified that dictates how the string should be interpreted. This pattern consists of various symbols, each representing a component of the date and/or time. Typical symbols include:
yfor yearMfor monthdfor dayHfor hourmfor minutesfor second
If the format pattern contains a character that is not recognized as a symbol for a date or time component, it is deemed "illegal," which will throw an error.
The Specific Case of 'T'
The character 'T' is a special case often encountered when dealing with ISO 8601 date formats. An ISO 8601 timestamp might look like this: 2023-09-28T15:30:00. In this format, 'T' is used as a delimiter separating the date from the time.
Unfortunately, SimpleDateFormat does not recognize 'T' as a pattern symbol. If you attempt to use it directly without understanding this limitation, you'll encounter an IllegalArgumentException with the message: "Illegal pattern character 'T'."
Example Case
In the above code, if we were to remove the single quotes around 'T', it would raise an error.
Solutions to the Problem
Quoting Literal Characters
To incorporate 'T' or any other non-pattern character into your date format string, you need to enclose it in single quotes ('). This tells SimpleDateFormat that 'T' is a literal character, not an element of the date itself.
Using Recommended Libraries
Java 8 and later versions include the java.time package, which provides a more robust solution for parsing and formatting dates. Specifically, you can use the DateTimeFormatter class, which understands ISO 8601 formats natively.
Using java.time with DateTimeFormatter
This code effectively handles the 'T' without requiring additional quoting or configuration, making it a preferable option when using Java 8 or newer.
Summary Table
| Concept | Details |
| Illegal Character | Occurs when 'T' is used in SimpleDateFormat without quotes. |
| Common Scenario | Encountered in ISO 8601 formatted date-times, which use 'T' as a date-time separator. |
| Solution: SimpleDateFormat | Enclose 'T' in single quotes ('T'). |
| Solution: DateTimeFormatter | Use Java's DateTimeFormatter with ISO 8601 support, available in Java 8+. |
| Recommended Practice | Prefer java.time over java.util.Date for date-time operations in Java 8+ due to better support for modern date-time formats and patterns. |
Additional Topics
Exceptions Handling in Date Parsing
When parsing dates, always handle potential exceptions such as ParseException in SimpleDateFormat or DateTimeParseException in DateTimeFormatter. This ensures your application can gracefully handle unexpected formats or data anomalies.
Performance Considerations
Using java.time is generally more efficient and less error-prone than working with SimpleDateFormat, especially when dealing with complex date-time manipulations or conversions.
Locale and Time Zone Adjustments
When working with dates and times, consider the locale and timezone effects, particularly for applications spanning multiple regions or countries. Java's java.time package offers enhanced support for managing these nuances effectively.
By understanding and addressing the Illegal pattern character 'T', Java developers can efficiently parse ISO 8601 and similar date formats, ensuring their applications manage temporal data robustly and accurately.
Related reading
- Illegal reflective access by org.springframework.cglib.core.ReflectUtils1
- Illegal reflective access when I stop SpringBoot web application with Tomcat 9 and Java10
- IllegalArgumentException Not a managed type in Spring Boot application
- IllegalArgumentException or NullPointerException for a null parameter?
- IllegalArgumentException navigation destination xxx is unknown to this NavController
- IllegalArgumentException /tmp/zookeeper/myid file is missing
- IllegalMonitorStateException on wait call
- IllegalMonitorStateException on wait call

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.