"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Parse Dates with \'Z\' Literals Using SimpleDateFormat?

How to Parse Dates with \'Z\' Literals Using SimpleDateFormat?

Published on 2024-11-08
Browse:434

How to Parse Dates with \'Z\' Literals Using SimpleDateFormat?

Dealing with 'Z' Literal in SimpleDateFormat Date Parsing

In the realm of date parsing, the 'Z' literal holds a special significance. It serves as a marker indicating that the time specified uses UTC as the reference point. However, parsing a date with this literal using SimpleDateFormat can pose challenges for some specific patterns.

As you have encountered, SimpleDateFormat struggles to interpret this format using patterns like "yyyy-MM-dd'T'HH:mm:ss" and its variants. While you can manually set the TimeZone on the SimpleDateFormat, this should not be a necessary measure.

The solution lies in employing a pattern that explicitly handles the 'Z' literal. In Java 7 and later, the appropriate pattern to use is "yyyy-MM-dd'T'HH:mm:ssX". This pattern will recognize the 'Z' literal as a timezone offset and appropriately parse the date in UTC format.

Therefore, to successfully parse the date "2010-04-05T17:16:00Z", you should utilize the following pattern:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX");
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3