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

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#setMillisecond() . 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: XMLGregorianCalendarImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
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: DateUtils.java    From training with MIT License 6 votes vote down vote up
public static XMLGregorianCalendar extractXmlDateTime(Date timestamp) {
	if (timestamp == null) {
		return null;
	}
	XMLGregorianCalendar date = null;
	try {
		GregorianCalendar startDate = new GregorianCalendar();
		startDate.setTime(timestamp);
		date = DatatypeFactory.newInstance().newXMLGregorianCalendar(startDate);
		date.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
		date.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return date;
}
 
Example 4
Source File: XMLGregorianCalendarImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 5
Source File: AbstractNetSuiteTestBase.java    From components with Apache License 2.0 6 votes vote down vote up
protected static XMLGregorianCalendar composeDateTime() throws Exception {
    DateTime dateTime = DateTime.now();

    XMLGregorianCalendar xts = datatypeFactory.newXMLGregorianCalendar();
    xts.setYear(dateTime.getYear());
    xts.setMonth(dateTime.getMonthOfYear());
    xts.setDay(dateTime.getDayOfMonth());
    xts.setHour(dateTime.getHourOfDay());
    xts.setMinute(dateTime.getMinuteOfHour());
    xts.setSecond(dateTime.getSecondOfMinute());
    xts.setMillisecond(dateTime.getMillisOfSecond());
    xts.setTimezone(dateTime.getZone().toTimeZone().getRawOffset() / 60000);

    return xts;

}
 
Example 6
Source File: XMLGregorianCalendarImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 7
Source File: XMLGregorianCalendarImpl.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 8
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 9
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 10
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 11
Source File: XMLGregorianCalendarImpl.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * <p>Normalize this instance to UTC.</p>
 *
 * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
 * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
 */
public XMLGregorianCalendar normalize() {

    XMLGregorianCalendar normalized = normalizeToTimezone(timezone);

    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }

    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }

    return normalized;
}
 
Example 12
Source File: XMLGregorianCalendarAsDateTime.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void createCalendar(Date date, XMLGregorianCalendar calendar) {
	calendar.setYear(date.getYear() + 1900);
	calendar.setMonth(date.getMonth() + 1);
	calendar.setDay(date.getDate());
	calendar.setHour(date.getHours());
	calendar.setMinute(date.getMinutes());
	calendar.setSecond(date.getSeconds());
	calendar.setMillisecond((int) (date.getTime() % 1000));
}
 
Example 13
Source File: XmlCteUtil.java    From Java_CTe with MIT License 5 votes vote down vote up
public static String dataCte(LocalDateTime dataASerFormatada, ZoneId zoneId) {
    try {
        GregorianCalendar calendar = GregorianCalendar.from(dataASerFormatada.atZone(ObjetoCTeUtil.verifica(zoneId).orElse(ZoneId.of("Brazil/East"))));

        XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        xmlCalendar.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
        return xmlCalendar.toString();

    } catch (DatatypeConfigurationException e) {
        LoggerUtil.log(XmlCteUtil.class, e.getMessage());
    }
    return null;
}
 
Example 14
Source File: FEELXMLGregorianCalendar.java    From jdmn with Apache License 2.0 5 votes vote down vote up
@Override
public XMLGregorianCalendar normalize() {
    XMLGregorianCalendar normalized = normalizeToTimezone(zoneID);
    // if timezone was undefined, leave it undefined
    if (getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
    }
    // if milliseconds was undefined, leave it undefined
    if (getMillisecond() == DatatypeConstants.FIELD_UNDEFINED) {
        normalized.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
    }
    return normalized;
}
 
Example 15
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTime() throws Exception {
    if (!shouldRunTest("Time")) {
        return;
    }
    javax.xml.datatype.DatatypeFactory datatypeFactory = javax.xml.datatype.DatatypeFactory.newInstance();

    XMLGregorianCalendar x = datatypeFactory.newXMLGregorianCalendar();
    x.setHour(12);
    x.setMinute(14);
    x.setSecond(5);
    XMLGregorianCalendar yOrig = datatypeFactory.newXMLGregorianCalendar();
    yOrig.setHour(22);
    yOrig.setMinute(4);
    yOrig.setSecond(15);
    yOrig.setMillisecond(250);

    Holder<XMLGregorianCalendar> y = new Holder<>(yOrig);
    Holder<XMLGregorianCalendar> z = new Holder<>();

    XMLGregorianCalendar ret;
    if (testDocLiteral) {
        ret = docClient.testTime(x, y, z);
    } else if (testXMLBinding) {
        ret = xmlClient.testTime(x, y, z);
    } else {
        ret = rpcClient.testTime(x, y, z);
    }
    if (!perfTestOnly) {
        assertTrue("testTime(): Incorrect value for inout param", equalsTime(x, y.value));
        assertTrue("testTime(): Incorrect value for out param", equalsTime(yOrig, z.value));
        assertTrue("testTime(): Incorrect return value", equalsTime(x, ret));
    }
}
 
Example 16
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
private XMLGregorianCalendar addYears(int amount, XMLGregorianCalendar cal) {
    cal.setYear(2000);
    cal.setMonth(6);
    cal.setDay(1);
    cal.setHour(0);
    cal.setMinute(0);
    cal.setSecond(0);
    cal.setMillisecond(0);
    return calendarUtil.addYears(cal, amount);
}
 
Example 17
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Test AddMonths into winterTime.
 * Result should be moths +2 and a one hour shift to the left.
 *
 */
@Test
public void testAddMonthsIntoWinterTime() {
    XMLGregorianCalendar cal = calendarUtil.buildXMLGregorianCalendar();
    cal.setMonth(10);
    cal.setDay(1);
    cal.setHour(0);
    cal.setMinute(0);
    cal.setSecond(0);
    cal.setMillisecond(0);
    XMLGregorianCalendar result = calendarUtil.addMonths(cal, 2);
    assertEquals(12, result.getMonth());
}
 
Example 18
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
/**
 * Test AddMonths into winterTime.
 * Result should be moths +2 and a one hour shift to the right.
 *
 */
@Test
public void testAddMonthsIntoSummerTime() {
    XMLGregorianCalendar cal = calendarUtil.buildXMLGregorianCalendar();
    cal.setMonth(2);
    cal.setDay(1);
    cal.setHour(0);
    cal.setMinute(0);
    cal.setSecond(0);
    cal.setMillisecond(0);
    XMLGregorianCalendar result = calendarUtil.addMonths(cal, 2);
    assertEquals(4, result.getMonth());
}
 
Example 19
Source File: AbstractXMLGregorianCalendarAdapter.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setMillisecond(Calendar source, XMLGregorianCalendar target) {
	final int millisecond = source.get(Calendar.MILLISECOND);
	if (millisecond != 0) {
		target.setMillisecond(millisecond);
	}
}
 
Example 20
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;
}