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

How to Format the Current Time in ISO 8601 (YYYY-MM-DDTHH:mmZ) Format?

Susan Sarandon
Susan SarandonOriginal
2024-11-25 03:10:14552browse

How to Format the Current Time in ISO 8601 (YYYY-MM-DDTHH:mmZ) Format?

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

Problem:

Obtain a timestamp in ISO 8601 format, reflecting the current moment in UTC, adhering to the format: 2010-10-12T08:50Z.

Solution:

Leveraging the flexibility of SimpleDateFormat, the following code snippet elegantly addresses the requirement:

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());

By utilizing a new Date() object, as demonstrated in the code above, the formatted timestamp corresponds to the current moment.

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