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!