Java Code Examples for javax.xml.datatype.XMLGregorianCalendar#getMillisecond()

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#getMillisecond() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GO_DateTime.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a wrapper for the given {@link Date}.
 *
 * @param date  the date to marshal. Can not be {@code null}.
 */
private GO_DateTime(final Date date) {
    final Context context = Context.current();
    try {
        final XMLGregorianCalendar gc = XmlUtilities.toXML(context, date);
        if (Context.isFlagSet(context, Context.LEGACY_METADATA)) {
            if (XmlUtilities.trimTime(gc, false)) {
                this.date = gc;
            } else {
                dateTime = gc;
            }
        } else {
            if (gc.getMillisecond() == 0) {
                gc.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
            }
            dateTime = gc;
        }
    } catch (DatatypeConfigurationException e) {
        Context.warningOccured(context, XmlAdapter.class, "marshal", e, true);
    }
}
 
Example 2
Source File: UniversalTimeAdapter.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Converts the date to the object to be marshalled in a XML file or stream.
 * JAXB calls automatically this method at marshalling time.
 *
 * @param  value  the {@code java.util} date value, or {@code null}.
 * @return the XML date, or {@code null}.
 */
@Override
public XMLGregorianCalendar marshal(final Date value) {
    if (value != null) {
        final GregorianCalendar calendar = new GregorianCalendar(UTC, Locale.ROOT);
        calendar.setTime(value);
        try {
            final XMLGregorianCalendar gc = XmlUtilities.getDatatypeFactory().newXMLGregorianCalendar(calendar);
            if (gc.getMillisecond() == 0) {
                gc.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
            }
            return gc;
        } catch (DatatypeConfigurationException e) {
            Context.warningOccured(Context.current(), XmlAdapter.class, "marshal", e, true);
        }
    }
    return null;
}
 
Example 3
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean equalsDate(XMLGregorianCalendar orig, XMLGregorianCalendar actual) {
    boolean result = false;

    if ((orig.getYear() == actual.getYear()) && (orig.getMonth() == actual.getMonth())
        && (orig.getDay() == actual.getDay()) && (actual.getHour() == DatatypeConstants.FIELD_UNDEFINED)
        && (actual.getMinute() == DatatypeConstants.FIELD_UNDEFINED)
        && (actual.getSecond() == DatatypeConstants.FIELD_UNDEFINED)
        && (actual.getMillisecond() == DatatypeConstants.FIELD_UNDEFINED)) {

        result = orig.getTimezone() == actual.getTimezone();
    }
    return result;
}
 
Example 4
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean equalsTime(XMLGregorianCalendar orig, XMLGregorianCalendar actual) {
    boolean result = false;
    if ((orig.getHour() == actual.getHour()) && (orig.getMinute() == actual.getMinute())
        && (orig.getSecond() == actual.getSecond()) && (orig.getMillisecond() == actual.getMillisecond())
        && (orig.getTimezone() == actual.getTimezone())) {
        result = true;
    }
    return result;
}
 
Example 5
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected boolean equalsDateTime(XMLGregorianCalendar orig, XMLGregorianCalendar actual) {
    boolean result = false;
    if ((orig.getYear() == actual.getYear()) && (orig.getMonth() == actual.getMonth())
        && (orig.getDay() == actual.getDay()) && (orig.getHour() == actual.getHour())
        && (orig.getMinute() == actual.getMinute()) && (orig.getSecond() == actual.getSecond())
        && (orig.getMillisecond() == actual.getMillisecond())) {

        result = orig.getTimezone() == actual.getTimezone();
    }
    return result;
}
 
Example 6
Source File: XmlUtilities.java    From sis with Apache License 2.0 3 votes vote down vote up
/**
 * Trims the time components of the given calendar if their values are zero, or leaves
 * them unchanged otherwise (except for milliseconds). More specifically:
 *
 * <ul>
 *   <li>If the {@code force} argument is {@code false}, then:
 *     <ul>
 *       <li>If every time components (hour, minute, seconds and milliseconds) are zero, set
 *           them to {@code FIELD_UNDEFINED} in order to prevent them from being formatted
 *           at XML marshalling time. Then returns {@code true}.</li>
 *       <li>Otherwise returns {@code false}. But before doing so, still set the milliseconds
 *           to {@code FIELD_UNDEFINED} if its value was 0.</li>
 *     </ul></li>
 *   <li>Otherwise (if the {@code force} argument is {@code false}), then the temporal
 *       part is set to {@code FIELD_UNDEFINED} unconditionally and this method returns
 *       {@code true}.</li>
 * </ul>
 *
 * <strong>WARNING: The timezone information may be lost!</strong> This method is used mostly
 * when the Gregorian Calendar were created from a {@link Date}, in which case we don't know
 * if the time is really 0 or just unspecified. This method should be invoked only when we
 * want to assume that a time of zero means "unspecified".
 *
 * <p>This method will be deprecated after we implemented ISO 19108 in SIS.</p>
 *
 * @param  gc     the date to modify in-place.
 * @param  force  {@code true} for forcing the temporal components to be removed without any check.
 * @return {@code true} if the time part has been completely removed, {@code false} otherwise.
 */
public static boolean trimTime(final XMLGregorianCalendar gc, final boolean force) {
    if (force || gc.getMillisecond() == 0) {
        gc.setMillisecond(FIELD_UNDEFINED);
        if (force || (gc.getHour() == 0 && gc.getMinute() == 0 && gc.getSecond() == 0)) {
            gc.setHour(FIELD_UNDEFINED);
            gc.setMinute(FIELD_UNDEFINED);
            gc.setSecond(FIELD_UNDEFINED);
            gc.setTimezone(FIELD_UNDEFINED);
            return true;
        }
    }
    return false;
}