如何获得ISO 8601格式的当前时刻,包括日期,小时和分钟?
问题描述:
获取当前时刻 ISO 8601 格式化演示文稿的最优雅方式是什么? ?它应该如下所示: 2010-10-12T08:50Z
。
What is the most elegant way to get ISO 8601 formatted presentation of current moment, UTC? It should look like: 2010-10-12T08:50Z
.
示例:
String iso8601 = DateFormat.getDateTimeInstance(DateFormat.ISO_8601).format(date);
答
使用 SimpleDateFormat
格式化任何 日期
你想要的对象:
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());
使用新日期()
如图所示以上将格式化当前时间。
Using a new Date()
as shown above will format the current time.