如何获取带有日期、小时和分钟的 ISO 8601 格式的当前时刻?
问题描述:
获取当前时刻的 ISO 8601 格式演示的最优雅方式是什么,世界标准时间?它应该看起来像:2010-10-12T08:50Z
.
What is the most elegant way to get ISO 8601 formatted presentation of the current moment, UTC? It should look like: 2010-10-12T08:50Z
.
示例:
String d = DateFormat.getDateTimeInstance(DateFormat.ISO_8601).format(date);
``
答
使用 SimpleDateFormat
格式化任何 Date
你想要的对象:
Use SimpleDateFormat
to format any Date
object you want:
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());
使用如上所示的 new Date()
将格式化当前时间.
Using a new Date()
as shown above will format the current time.