Example usage for javax.xml.datatype DatatypeFactory newInstance

List of usage examples for javax.xml.datatype DatatypeFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.datatype DatatypeFactory newInstance.

Prototype

public static DatatypeFactory newInstance() throws DatatypeConfigurationException 

Source Link

Document

Obtain a new instance of a DatatypeFactory .

Usage

From source file:com.aes.touresbalon.touresbalonoms.utilities.OmsUtil.java

public static XMLGregorianCalendar stringToXMLGreogrianCalendar(Date fecha)
        throws ParseException, DatatypeConfigurationException {
    //        DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    //        Date fechaUtil = formatter.parse(fecha);
    GregorianCalendar gc = new GregorianCalendar();
    DatatypeFactory df = DatatypeFactory.newInstance();
    gc.setTimeInMillis(fecha.getTime());
    return df.newXMLGregorianCalendar(gc);
}

From source file:Main.java

/**
 *  Convert date from String to Xml GregorianCalendar in this 'MM/dd/yyyy hh:mm:ss'
 * @param s//from w w w.ja  va2  s  . c  o m
 * @return
 * @throws DatatypeConfigurationException
 * @throws ParseException
 */
public static XMLGregorianCalendar stringToXMLGregorianCalendar(String s)
        throws DatatypeConfigurationException, ParseException {
    if (s != null && !(s.trim().length() < 1)) {
        XMLGregorianCalendar result = null;
        Date date;
        SimpleDateFormat simpleDateFormat;
        GregorianCalendar gregorianCalendar;

        simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
        date = simpleDateFormat.parse(s);
        gregorianCalendar = (GregorianCalendar) GregorianCalendar.getInstance();
        gregorianCalendar.setTime(date);
        result = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
        return result;

    }
    return null;
}

From source file:Main.java

public static XMLGregorianCalendar toGregorianCaldendar(final Date date) {
    if (date != null) {
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(date);//from w w  w .j a va2 s . c o m
        try {
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    return null;
}

From source file:Main.java

public static XMLGregorianCalendar convertDateToXmlDate(Date date) {
    try {/*from   w w w  . ja  va 2s  .c  o  m*/
        GregorianCalendar fromDate = new GregorianCalendar();
        fromDate.setTime(date);
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(fromDate);
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Returns a date instance for a given timestamp string that complies to the
 * RFC 822 standard/*from w ww.j av  a  2s  .  c  o m*/
 * 
 * @param rfc822DateString
 * @return
 * @throws DatatypeConfigurationException
 */
public static Date getDate(String rfc822DateString) throws DatatypeConfigurationException {
    // SimpleDateFormat can't handle RFC822 (-04:00 instead of GMT-04:00)
    // date formats
    //
    // SimpleDateFormat dateFormat = new
    // SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    // return dateFormat.parse(propertyNode.asString());
    GregorianCalendar calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(rfc822DateString)
            .toGregorianCalendar();
    return calendar.getTime();
}

From source file:Main.java

public static XMLGregorianCalendar parse(long time, int timezone) {
    GregorianCalendar cal = new GregorianCalendar();

    cal.setTimeInMillis(time);//from w w  w  . j a  v  a 2 s  .  c  om
    XMLGregorianCalendar xCal = null;
    try {
        xCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
        xCal.setTimezone(timezone);
    } catch (DatatypeConfigurationException e) {
        e.printStackTrace();
    }

    return xCal;
}

From source file:Main.java

/**
 * Converts a java.util.Date to a javax.xml.datatype.XMLGregorianCalendar, uses current time if specified date is null;
 * //from  w  w  w .  j a v a  2s  .com
 * @param date, uses current time if specified date is null
 * @return a corresponding XMLGregorianCalendar object
 */
public static XMLGregorianCalendar convertDateToXmlDate(Date date) {
    try {
        GregorianCalendar fromDate = new GregorianCalendar();
        if (date != null) {
            fromDate.setTime(date);
        }
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(fromDate);
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Convert the {@link Date} into a {@link XMLGregorianCalendar}.
 * /* w w w.  ja  va2  s.  c o  m*/
 * Calls {@link DatatypeFactory#newInstance()} - if a {@link DatatypeConfigurationException}
 * is thrown it get's wrapped in the unchecked {@link IllegalStateException}.
 * 
 * @param date
 * @return the converted calendar
 * @throws IllegalStateException wrapping a {@link DatatypeConfigurationException}.
 */
public static XMLGregorianCalendar convertDateToXMLGregorianCalendar(final Date date) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    try {
        return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
    } catch (DatatypeConfigurationException e) {
        throw new IllegalStateException("unable to invoke DatatypeFactory.newInstance", e);
    }
}

From source file:Main.java

/**
 * Gets a static instance of a JAXP DatatypeFactory.
 * //  w  w  w .j  a  v a 2s .  c o m
 * @return the factory or null if the factory could not be created
 */
public static DatatypeFactory getDataTypeFactory() {
    if (dataTypeFactory == null) {
        try {
            dataTypeFactory = DatatypeFactory.newInstance();
        } catch (DatatypeConfigurationException e) {
            // do nothing
        }
    }

    return dataTypeFactory;
}

From source file:com.stgmastek.core.util.CommonUtils.java

/**
 * A new method, that contains the code previously present in the above
 * method, that is, to create an XML GC Object with both the Date and time
 * component.//ww  w  .  j  a v  a2s  . com
 */
public static XMLGregorianCalendar getXMLDateTime(Date date) throws DatatypeConfigurationException {
    GregorianCalendar gCal = new GregorianCalendar();
    if (date != null) {
        gCal.setTime(date);
        XMLGregorianCalendar xgCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gCal);
        return xgCal;
    } else {
        return null;
    }
}