Java XML Date Create dateToString(Calendar calendar)

Here you can find the source of dateToString(Calendar calendar)

Description

Formats the given date into its string representation according to the ISO 8601 (e.g.

License

EUPL

Parameter

Parameter Description
calendar the date to be formatted

Return

formatted date as specified in ISO 8601

Declaration

public static String dateToString(Calendar calendar) 

Method Source Code


//package com.java2s;
/*// ww w .j  a v a 2 s  . c om
* Copyright 2013 by the digital.me project (http://www.dime-project.eu).
*
* Licensed under the EUPL, Version 1.1 only (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://joinup.ec.europa.eu/software/page/eupl/licence-eupl
*
* Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and limitations under the Licence.
*/

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    private static DatatypeFactory dtFactory;

    /**
     * Formats the given date into its string representation
     * according to the ISO 8601 (e.g. 2003-01-22).
     * Time zone is ignored.
     * 
     * @param calendar the date to be formatted
     * @return formatted date as specified in ISO 8601
     */
    public static String dateToString(Calendar calendar) {
        GregorianCalendar utcCalendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
        utcCalendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));

        XMLGregorianCalendar xmlCalendar = dtFactory.newXMLGregorianCalendar(utcCalendar);
        xmlCalendar = xmlCalendar.normalize();
        return xmlCalendar.toXMLFormat();
    }
}

Related

  1. createXMLGregorianCalendar(Date currentDateTime)
  2. createXmlGregorianCalendar(Date date)
  3. createXMLGregorianCalendar(final Date date)
  4. createXMLGregorianCalendar(final Date time)
  5. dateToXml(Date date)
  6. DateToXmlCalendar(Date date)
  7. dateToXmlDate(Date date)
  8. dateToXMLGC(Date fecha)