Java Date XML Format toXMLDateTime(final Date date)

Here you can find the source of toXMLDateTime(final Date date)

Description

Transform a Date object to a String formatted according to the specification of the <code>dateTime</code> datatype of XML schema.<br> See <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">section 3.2.7 of the XML Specification</a> for details.

License

Open Source License

Parameter

Parameter Description
date The date as Calendar object to convert to String.

Return

The date as an xs:dateTime formatted String or null when date object was null

Declaration

public static String toXMLDateTime(final Date date) 

Method Source Code

//package com.java2s;
/**//from   ww w  .  j  a  va  2s.  c  om
 * Copyright (C) 2014 The Holodeck B2B Team, Sander Fieten
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.TimeZone;

public class Main {
    /** 
     * Transform a {@link Date} object to a {@link String} formatted according to
     * the specification of the <code>dateTime</code> datatype of XML schema.<br>
     * See <a href="http://www.w3.org/TR/xmlschema-2/#dateTime">section 3.2.7 of the XML
     * Specification</a> for details.
     * 
     * @param   date  The date as Calendar object to convert to String.
     * @return  The date as an <code>xs:dateTime</code> formatted String 
     *          or <code>null</code> when date object was <code>null</code>
     */
    public static String toXMLDateTime(final Date date) {
        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);
    }
}

Related

  1. formatXml(Date date)
  2. formatXMLDate(Date date, String pattern)
  3. getTime(String xmlDateTime)
  4. getXmlTime(Date date)
  5. parse(String xmlDateTime)
  6. xml2Date(String xmlDate)
  7. XML2HL7(String XMLDate)
  8. xmlSerialize(Date date)