I need to get the current date in ISO 8601 with SS milliseconds and HH:MM timezone
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)
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 result: 2012-11-24T21:19:27.758+0000
Apache result: 2012-11-24T21:19:27.758+00:00 (Apache Commons FastDateFormat)
解决方案
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