Home > Java > javaTutorial > Why Does Parsing a Date String with 'T' Throw an 'Illegal Pattern Character 'T'' Error in Java?

Why Does Parsing a Date String with 'T' Throw an 'Illegal Pattern Character 'T'' Error in Java?

Patricia Arquette
Release: 2024-12-11 02:16:09
Original
174 people have browsed it

Why Does Parsing a Date String with

Illegal Pattern Character 'T' When Parsing a Date String to java.util.Date

When attempting to parse a date string into a java.util.Date object using the SimpleDateFormat, developers may encounter the error "Illegal pattern character 'T'". This exception often arises due to mismatched formatting in the pattern used by SimpleDateFormat.

In most circumstances, the 'T' character should be included in the pattern if it exists in the input date string. It represents the transition between the date and time components and ensures proper parsing. In the provided example:

"yyyy-MM-ddThh:mm:ssZ"
Copy after login

The correct pattern should be:

"yyyy-MM-dd'T'hh:mm:ssZ"
Copy after login

where the 'T' character is enclosed in single quotes to escape it from being treated as the literal character 'T'.

Additionally, the pattern for the 'Z' suffix, representing Zulu or Coordinated Universal Time (UTC), is not simply 'Z', but rather 'XXX'. This is explained in the SimpleDateFormat documentation, though it can be confusing since 'Z' is also used for TimeZone information.

The above is the detailed content of Why Does Parsing a Date String with 'T' Throw an 'Illegal Pattern Character 'T'' Error in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template