Example usage for javax.xml.datatype XMLGregorianCalendar getMillisecond

List of usage examples for javax.xml.datatype XMLGregorianCalendar getMillisecond

Introduction

In this page you can find the example usage for javax.xml.datatype XMLGregorianCalendar getMillisecond.

Prototype

public int getMillisecond() 

Source Link

Document

Returns the millisecond precision of #getFractionalSecond() .

Usage

From source file:Main.java

public static DateTime xmlToDateTime(final XMLGregorianCalendar xmlCal) {
    DateTime result = null;/* www.  j ava 2  s .  com*/
    if (xmlCal != null) {
        int hourOfDay = xmlCal.getHour();
        int minuteOfHour = xmlCal.getMinute();
        int secondOfMinute = xmlCal.getSecond();
        int milliOfSecond = xmlCal.getMillisecond();

        if (hourOfDay < 0) {
            hourOfDay = 0;
        }
        if (minuteOfHour < 0) {
            minuteOfHour = 0;
        }
        if (secondOfMinute < 0) {
            secondOfMinute = 0;
        }
        if (milliOfSecond < 0) {
            milliOfSecond = 0;
        }
        result = new DateTime(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay(), hourOfDay, minuteOfHour,
                secondOfMinute, milliOfSecond);
    }
    return result;
}

From source file:Main.java

/**
 * Converts from an XMLGregorianCalendar to a LocalTime i.e. represents a time without timezone or date information.
 * //  w w w. ja  va 2 s  . com
 * @param calendar XMLGregorianCalendar object.
 * @return The calendar converted to a LocalTime.
 */
public static LocalTime convertToLocalTime(XMLGregorianCalendar calendar) {
    if (calendar == null) {
        return null;
    }
    int hour = calendar.getHour() > 0 ? calendar.getHour() : 0;
    int minute = calendar.getMinute() > 0 ? calendar.getMinute() : 0;
    int second = calendar.getSecond() > 0 ? calendar.getSecond() : 0;
    int millisecond = calendar.getMillisecond() > 0 ? calendar.getMillisecond() : 0;

    return new LocalTime(hour, minute, second, millisecond);
}

From source file:Main.java

/**
 * Converts from an XMLGregorianCalendar to a LocalDateTime i.e. represents a date and time without timezone
 * inform.ation// w ww .j a v  a 2 s  .co  m
 * 
 * @param calendar XMLGregorianCalendar object.
 * @return The calendar converted to a LocalDateTime.
 */
public static LocalDateTime convertToLocalDateTime(XMLGregorianCalendar calendar) {
    if (calendar == null) {
        return null;
    }
    int year = calendar.getYear() > 0 ? calendar.getYear() : 0;
    int hour = calendar.getHour() > 0 ? calendar.getHour() : 0;
    int minute = calendar.getMinute() > 0 ? calendar.getMinute() : 0;
    int second = calendar.getSecond() > 0 ? calendar.getSecond() : 0;
    int millisecond = calendar.getMillisecond() > 0 ? calendar.getMillisecond() : 0;

    return new LocalDateTime(year, calendar.getMonth(), calendar.getDay(), hour, minute, second, millisecond);
}

From source file:Main.java

/**
 * Converts an XMLGregorianCalendar to a Date.
 *
 * @param xmlDate/*from   w  w w .  j  a v a  2  s  .c  o m*/
 *            XMLGregorianCalendar to convert.
 * @return corresponding date object.
 */
public static Date getDate(final XMLGregorianCalendar xmlDate) {
    // TODO: is this equivalent to getDate(String) processing above??

    // start with UTC, i.e. no daylight savings time.
    TimeZone timezone = TimeZone.getTimeZone("GMT");

    // adjust timezone to match xmldate
    int offsetMinutes = xmlDate.getTimezone();
    if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) {
        timezone.setRawOffset(
                // convert minutes to milliseconds
                offsetMinutes * 60 // seconds per minute
                        * 1000 // milliseconds per second
        );
    }

    // use calendar so parsed date will be UTC
    Calendar calendar = Calendar.getInstance(timezone);
    calendar.clear();
    calendar.set(xmlDate.getYear(),
            // xmlcalendar is 1 based, calender is 0 based
            xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(),
            xmlDate.getSecond());
    Date date = calendar.getTime();
    int millis = xmlDate.getMillisecond();
    if (millis != DatatypeConstants.FIELD_UNDEFINED) {
        calendar.setTimeInMillis(calendar.getTimeInMillis() + millis);
    }

    return date;
}

From source file:net.servicefixture.converter.XMLGregorianCalendarConverter.java

public String toString(Object source) {
    XMLGregorianCalendar src = (XMLGregorianCalendar) source;
    SimpleDateFormat formatter = new SimpleDateFormat(DateConverter.DATE_FORMAT);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, src.getYear());
    cal.set(Calendar.MONTH, src.getMonth() - 1);
    cal.set(Calendar.DAY_OF_MONTH, src.getDay());
    cal.set(Calendar.HOUR, src.getHour());
    cal.set(Calendar.MINUTE, src.getMinute());
    cal.set(Calendar.SECOND, src.getSecond());
    cal.set(Calendar.MILLISECOND, src.getMillisecond());
    return formatter.format(cal.getTime());
}

From source file:org.openvpms.esci.adapter.map.invoice.UBLInvoice.java

/**
 * Returns the invoice issue date/time.//from  w  ww.j a  v  a 2s  .  c  om
 *
 * @return the issue date/time
 * @throws ESCIAdapterException if the issue date isn't set
 */
public Date getIssueDatetime() {
    IssueDateType issueDate = getRequired(invoice.getIssueDate(), "IssueDate");
    XMLGregorianCalendar calendar = issueDate.getValue();
    checkRequired(calendar, "IssueDate");

    IssueTimeType issueTime = invoice.getIssueTime();
    if (issueTime != null) {
        XMLGregorianCalendar time = issueTime.getValue();
        if (time != null) {
            calendar.setHour(time.getHour());
            calendar.setMinute(time.getMinute());
            calendar.setSecond(time.getSecond());
            calendar.setMillisecond(time.getMillisecond());
        }
    }
    return calendar.toGregorianCalendar().getTime();
}

From source file:se.skl.skltpservices.npoadapter.mapper.util.EHRUtil.java

/**
 * Returns a {@link Date} date and time representation.
 *
 * @param cal/*w  w w  .  jav a2 s .c  o m*/
 *            the actual date and time.
 * @return the {@link Date} representation.
 */
public static Date toDate(XMLGregorianCalendar cal) {
    if (cal != null) {
        final Calendar c = Calendar.getInstance();

        c.set(Calendar.DATE, cal.getDay());
        c.set(Calendar.MONTH, cal.getMonth() - 1);
        c.set(Calendar.YEAR, cal.getYear());
        c.set(Calendar.DAY_OF_MONTH, cal.getDay());
        c.set(Calendar.HOUR_OF_DAY, cal.getHour());
        c.set(Calendar.MINUTE, cal.getMinute());
        c.set(Calendar.SECOND, cal.getSecond());
        c.set(Calendar.MILLISECOND, cal.getMillisecond());

        return c.getTime();
    }
    return null;
}