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
Release: 2024-11-25 03:10:14
Original
523 people have browsed it

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

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!

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