Home >Java >javaTutorial >How to Precisely Format the Current Moment in ISO 8601 UTC?

How to Precisely Format the Current Moment in ISO 8601 UTC?

DDD
DDDOriginal
2024-11-28 18:55:12882browse

How to Precisely Format the Current Moment in ISO 8601 UTC?

Precise ISO 8601 Formatting for Current Moment

Achieving an accurate ISO 8601 representation of the current moment in UTC poses a query to find the most refined approach. The desired format, 2010-10-12T08:50Z, necessitates a comprehensive solution.

To address this, we implore the capabilities of SimpleDateFormat. This versatile utility permits formatting of any Date object to meet our specifications.

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

By utilizing a new Date() instance, as demonstrated in the snippet, you can effortlessly capture the current moment and transform it into the desired ISO 8601 format.

The above is the detailed content of How to Precisely Format the Current Moment in ISO 8601 UTC?. 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