Java XML Date Create toDateTime(Date date)

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

Description

Convert a Java date to its XML date/time equivalent.

License

LGPL

Parameter

Parameter Description
date the Java date to convert

Return

an XML date/time

Declaration

public static XMLGregorianCalendar toDateTime(Date date) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.*;
import javax.xml.datatype.*;

public class Main {
    private static DatatypeFactory datatypeFactory;

    /**//w  ww .  ja  v a 2s. c  om
     * Convert a Java date to its XML date/time equivalent.
     *
     * @param date the Java date to convert
     * @return an XML date/time
     */
    public static XMLGregorianCalendar toDateTime(Date date) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        return datatypeFactory().newXMLGregorianCalendar(cal).normalize();
    }

    /**
     * Convert milliseconds offset to its XML date/time equivalent.
     *
     * @param millis a millisecond count since the epoch
     * @return an XML date/time
     */
    public static XMLGregorianCalendar toDateTime(long millis) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTimeInMillis(millis);
        return datatypeFactory().newXMLGregorianCalendar(cal).normalize();
    }

    /**
     * Return a shared instance of a datatype factory, used for creating new XML data objects.
     *
     * @return a shared datatype factory
     */
    public static DatatypeFactory datatypeFactory() {
        return datatypeFactory;
    }
}

Related

  1. toDate(XMLGregorianCalendar date)
  2. toDate(XMLGregorianCalendar dateTime)
  3. toDate(XMLGregorianCalendar xmlCalendar)
  4. toDate(XMLGregorianCalendar xmlGregorianCalendar)
  5. toDateTime()
  6. toXMLCalendar(Date date)
  7. toXmlDate(Date date)
  8. toXMLDate(Date date)
  9. toXmlDate(Date date)