Java XML Date Create toXmlDate(Date date)

Here you can find the source of toXmlDate(Date date)

Description

Returns a XMLGregorianCalender (xml:date) in current timezone based upon the given date only

License

Open Source License

Parameter

Parameter Description
date the date to convert

Return

date e.g. 2012-03-14

Declaration

public static XMLGregorianCalendar toXmlDate(Date date) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    private static DatatypeFactory factory = null;

    /**/*from  w  w w.  ja  va  2  s  . c o  m*/
     * Returns a XMLGregorianCalender (xml:date) in current timezone based upon the given date only
     *
     * @param date the date to convert
     * @return date e.g. 2012-03-14
     */
    public static XMLGregorianCalendar toXmlDate(Date date) {
        if (date != null) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);
            XMLGregorianCalendar xmlGregorianCalendar = factory.newXMLGregorianCalendar(cal);
            xmlGregorianCalendar.setHour(DatatypeConstants.FIELD_UNDEFINED);
            xmlGregorianCalendar.setMinute(DatatypeConstants.FIELD_UNDEFINED);
            xmlGregorianCalendar.setSecond(DatatypeConstants.FIELD_UNDEFINED);
            xmlGregorianCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
            xmlGregorianCalendar.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
            return xmlGregorianCalendar;
        } else {
            return null;
        }
    }
}

Related

  1. toDateTime()
  2. toDateTime(Date date)
  3. toXMLCalendar(Date date)
  4. toXmlDate(Date date)
  5. toXMLDate(Date date)
  6. toXmlDate(DateTime cal)
  7. toXmlDate(GregorianCalendar date)
  8. toXmlDateTime(GregorianCalendar cal)
  9. toXmlDateTimeUTC(Date date)