問題描述
我需要在 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 格式獲取當前日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!