Java Utililty Methods Date XML Format

List of utility methods to do Date XML Format

Description

The list of methods to do Date XML Format are organized into topic(s).

Method

StringformatDateToXML(Date dateTime)
format Date To XML
return formatDate(dateTime, XML_DATE_TIME_FORMAT);
StringformatToXMLDate(Date date)
format To XML Date
try {
    return new SimpleDateFormat("yyyyMMdd").format(date);
} catch (Exception e) {
    return null;
StringformatXml(Date date)
Convert date to XML format
return formatXml(date, null);
StringformatXMLDate(Date date, String pattern)
format XML Date
StringBuffer sb = new StringBuffer();
addXMLHeader(sb);
sb.append("<Date pattern=\"");
sb.append(pattern);
sb.append("\" >");
SimpleDateFormat fmt = new SimpleDateFormat(pattern);
sb.append(fmt.format(date));
sb.append("</Date>");
...
longgetTime(String xmlDateTime)
Gets the time, in milliseconds, from an XML date time string as defined at http://www.w3.org/TR/xmlschema-2/#dateTime
ParsePosition position = new ParsePosition(0);
Date date = ISO_8601_BASE.parse(xmlDateTime, position);
if (date == null) {
    throw new IllegalArgumentException("Invalid XML dateTime value: " + xmlDateTime + " (at position "
            + position.getErrorIndex() + ")");
Matcher matcher = ISO_8601_EXTRAS.matcher(xmlDateTime.substring(position.getIndex()));
if (!matcher.matches()) {
...
StringgetXmlTime(Date date)
get Xml Time
return fixTimezone((new SimpleDateFormat(XML_TIME_FORMAT)).format(date));
Dateparse(String xmlDateTime)
parse
if (xmlDateTime.length() != 25) {
    throw new ParseException("Date not in expected xsd:datetime format", 0);
StringBuilder sb = new StringBuilder(xmlDateTime);
sb.deleteCharAt(22);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(XSD_DATETIME);
return simpleDateFormat.parse(sb.toString());
StringtoXMLDateTime(final Date date)
Transform a Date object to a String formatted according to the specification of the dateTime datatype of XML schema.
See section 3.2.7 of the XML Specification for details.
if (date == null)
    return null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXX");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
return formatter.format(date);
Datexml2Date(String xmlDate)
xml Date
return xmlDateFormat.parse(xmlDate);
StringXML2HL7(String XMLDate)
XMLHL
String out = "";
Date d;
try {
    d = XMLDateFmt.parse(XMLDate);
    out = HL7DateFmt.format(d);
} catch (Exception exp) {
    System.err.println(exp.getMessage());
return out;