Java XML Date Create getXmlGregorianCalendar(String unixTime)

Here you can find the source of getXmlGregorianCalendar(String unixTime)

Description

Turns a string representation of unix time into a XMLGregorianCalendar.

License

Apache License

Parameter

Parameter Description
unixTime The unixtime. If the argument is null, then epoch is returned.

Return

The XMLGregorianCalendar.

Declaration

public static XMLGregorianCalendar getXmlGregorianCalendar(String unixTime) 

Method Source Code


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

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**//from  www. jav  a2 s.  com
     * Turns a string representation of unix time into a XMLGregorianCalendar.
     * @param unixTime The unixtime. If the argument is null, then epoch is returned.
     * @return The XMLGregorianCalendar.
     */
    public static XMLGregorianCalendar getXmlGregorianCalendar(String unixTime) {
        return getXmlGregorianCalendar(getDate(unixTime));
    }

    /**
     * Turns a date into a XMLGregorianCalendar.
     * @param date The Date. If the argument is null, then epoch is returned.
     * @return The XMLGregorianCalendar.
     */
    public static XMLGregorianCalendar getXmlGregorianCalendar(Date date) {
        if (date == null) {
            date = new Date(0);
        }

        GregorianCalendar gc = new GregorianCalendar();
        try {
            gc.setTime(date);
            return DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
        } catch (Exception e) {
            throw new IllegalStateException("Could not convert the date '" + date + "' into the xml format.", e);
        }
    }

    /**
     * Creates a Date object from the string representation of unixtime in seconds. 
     * @param unixTime the unixtime. If the argument is null, then epoch is returned. 
     */
    public static Date getDate(String unixTime) {
        Date date;

        if (unixTime == null) {
            date = new Date(0);
        } else {
            date = new Date(Long.parseLong(unixTime) * 1000);
        }
        return date;
    }
}

Related

  1. getXMLFormat(Calendar calendar)
  2. getXmlGregCal(Date dt)
  3. getXMLGregorianCalendar()
  4. getXMLGregorianCalendar(Date date)
  5. getXMLGregorianCalendar(Date date)
  6. getXMLGregorianCalendar(TimeZone timeZone, Locale locale)
  7. getXMLGregorianCalendarFromDate(Date date)
  8. getXMLGregorianCalendarFromDate(Date date)
  9. getXMLGregorianCalendarValue( Date date)