Home > Java > javaTutorial > How to Get the Current Time in ISO 8601 Format (yyyy-MM-dd\'T\'HH:mm\'Z\')?

How to Get the Current Time in ISO 8601 Format (yyyy-MM-dd\'T\'HH:mm\'Z\')?

Susan Sarandon
Release: 2024-11-29 19:22:12
Original
188 people have browsed it

How to Get the Current Time in ISO 8601 Format (yyyy-MM-dd'T'HH:mm'Z')?

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

The query pertains to retrieving the current moment in ISO 8601 format, which follows the structure of "2010-10-12T08:50Z." This format represents a specific moment in time, including the date, hour, minute, and UTC offset.

To achieve this, the preferred approach utilizes the SimpleDateFormat class, which allows for the formatting of any Date object. By specifying a specific format string, you can control the output format.

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

In the code, note the following:

  • TimeZone tz: Represents the UTC timezone.
  • DateFormat df: An instance of SimpleDateFormat configured to format a Date object using the specified format string.
  • df.setTimeZone(tz): Sets the timezone for the formatter to UTC.
  • nowAsISO: Contains the current time in ISO 8601 format as a string.

The above is the detailed content of How to Get the Current Time in ISO 8601 Format (yyyy-MM-dd\'T\'HH:mm\'Z\')?. 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