Home >Java >javaTutorial >Does `SimpleDateFormat('yyyy-MM-dd'T'HH:mm:ss'Z'')` Automatically Handle Time Zones?

Does `SimpleDateFormat('yyyy-MM-dd'T'HH:mm:ss'Z'')` Automatically Handle Time Zones?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-04 03:04:091004browse

Does `SimpleDateFormat(

SimpleDateFormat with "yyyy-MM-dd'T'HH:mm:ss'Z'" Does Not Automatically Set Timezone

The Java SimpleDateFormat constructor:

SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")

by itself does not set the timezone. Adding a 'Z' to the end of the date/time string merely indicates a GMT/UTC timezone, but does not actually change the underlying date/time value.

To ensure that the parsed date/time is in GMT/UTC, you must explicitly set the timezone.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = sdf.parse("2013-09-29T18:46:19Z");

By setting the timezone to GMT, the parsed date/time will be converted to GMT and displayed correctly.

The above is the detailed content of Does `SimpleDateFormat('yyyy-MM-dd'T'HH:mm:ss'Z'')` Automatically Handle Time Zones?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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