Home >Java >javaTutorial >How to Precisely Format the Current Moment in ISO 8601 UTC?
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!