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
Release: 2024-12-04 03:04:09
Original
793 people have browsed it

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'")
Copy after login

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");
Copy after login

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!

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