Java:如何以 ISO 8601 SECOND 格式获取当前日期 [英] Java: How to get current date in ISO 8601 SECOND format

查看:30
本文介绍了Java:如何以 ISO 8601 SECOND 格式获取当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 ISO 8601 中以 SS 毫秒HH:MM 时区

I need to get the current date in ISO 8601 with SS milliseconds and HH:MM timezone

完整的日期加上小时、分钟、秒和 a 的小数部分第二:YYYY-MM-DDThh:mm:ss.sTZD(例如 1997-07-16T19:20:30.45+01:00)

Complete date plus hours, minutes, seconds and a decimal fraction of a second: YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

参见:http://www.w3.org/TR/NOTE-datetime

我尝试了不同的方法,但我无法获得正确的毫秒数&时区数字:

i tried different approaches, but i cannot get the correct milliseconds & timezone digits:

DateFormat df=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSZZ");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
df.format(new Date());

JAVA 结果: 2012-11-24T21:19:27.758+0000

JAVA result: 2012-11-24T21:19:27.758+0000

Apache 结果: 2012-11-24T21:19:27.758+00:00(Apache Commons FastDateFormat)

Apache result: 2012-11-24T21:19:27.758+00:00 (Apache Commons FastDateFormat)

推荐答案

其实JDK中有一个类处理ISO8601的解析和格式化:

Actually there is a class in JDK that handles parsing and formatting ISO8601:

import javax.xml.bind.DatatypeConverter;

DatatypeConverter.printDateTime(new GregorianCalendar())
//2012-11-24T22:42:03.142+01:00

这篇关于Java:如何以 ISO 8601 SECOND 格式获取当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆