Java tutorial
//package com.java2s; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; public class Main { /** Format used for the XML Schema timeInstant data type with milliseconds. */ public static final String TIME_INSTANT_FORMAT = "yyyy-MM-dd'T'HH':'mm':'ss'.'SSSS'Z'"; public static String encodeTimeInstant(Date aDate) { TimeZone utcTimeZone = TimeZone.getTimeZone("UTC"); SimpleDateFormat formatter = new SimpleDateFormat(TIME_INSTANT_FORMAT); formatter.setTimeZone(utcTimeZone); return formatter.format(aDate); } public static void encodeTimeInstant(Document doc, Element parent, Date date, String namespaceURI, String qualifiedName) { if (date != null) { encodeString(doc, parent, encodeTimeInstant(date), namespaceURI, qualifiedName); } } public static void encodeString(Document doc, Element parent, String str, String namespaceURI, String qualifiedName) { if (str != null) { Element elm = doc.createElementNS(namespaceURI, qualifiedName); Text strVal = doc.createTextNode(str); elm.appendChild(strVal); parent.appendChild(elm); } } }