Home > Java > javaTutorial > How Can I Get the Current UTC Time in ISO 8601 Format (YYYY-MM-DDTHH:mmZ)?

How Can I Get the Current UTC Time in ISO 8601 Format (YYYY-MM-DDTHH:mmZ)?

Susan Sarandon
Release: 2024-11-25 02:48:11
Original
1043 people have browsed it

How Can I Get the Current UTC Time in ISO 8601 Format (YYYY-MM-DDTHH:mmZ)?

Retrieving Current Moment in ISO 8601 Format with Date, Hour, and Minute

The question posed inquires about the most sophisticated method for obtaining the current moment in ISO 8601 format, specifically UTC, with the desired output being in the format: 2010-10-12T08:50Z.

Elegant Solution

The most elegant solution to this problem is to utilize the SimpleDateFormat class, which allows you to format any Date object. Here's how you can achieve this:

TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
df.setTimeZone(tz);
String nowAsISO = df.format(new Date());
Copy after login

By utilizing a new Date() object as shown above, you can conveniently format the current time in the desired ISO 8601 format.

The above is the detailed content of How Can I Get the Current UTC Time in ISO 8601 Format (YYYY-MM-DDTHH:mmZ)?. 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