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

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#setHour() . 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: PerfManagerMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
/**
 * Converts Calendar object into XMLGregorianCalendar
 *
 * @param calendar Object to be converted
 * @return XMLGregorianCalendar
 */
private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar calendar) throws DatatypeConfigurationException {

    DatatypeFactory dtf = DatatypeFactory.newInstance();
    XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar();
    xgc.setYear(calendar.get(Calendar.YEAR));
    xgc.setMonth(calendar.get(Calendar.MONTH) + 1);
    xgc.setDay(calendar.get(Calendar.DAY_OF_MONTH));
    xgc.setHour(calendar.get(Calendar.HOUR_OF_DAY));
    xgc.setMinute(calendar.get(Calendar.MINUTE));
    xgc.setSecond(calendar.get(Calendar.SECOND));
    xgc.setMillisecond(calendar.get(Calendar.MILLISECOND));

    // Calendar ZONE_OFFSET and DST_OFFSET fields are in milliseconds.
    int offsetInMinutes = (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000);
    xgc.setTimezone(offsetInMinutes);
    return xgc;
}
 
Example 2
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 3
Source File: Docx4j_创建批注_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public XMLGregorianCalendar toXMLCalendar(Date d) throws Exception {  
    GregorianCalendar gc = new GregorianCalendar();  
    gc.setTime(d);  
    XMLGregorianCalendar xml = DatatypeFactory.newInstance()  
            .newXMLGregorianCalendar();  
    xml.setYear(gc.get(Calendar.YEAR));  
    xml.setMonth(gc.get(Calendar.MONTH) + 1);  
    xml.setDay(gc.get(Calendar.DAY_OF_MONTH));  
    xml.setHour(gc.get(Calendar.HOUR_OF_DAY));  
    xml.setMinute(gc.get(Calendar.MINUTE));  
    xml.setSecond(gc.get(Calendar.SECOND));  
    return xml;  
}
 
Example 4
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 5
Source File: AbstractTypeTestClient.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateTime() throws Exception {
    if (!shouldRunTest("DateTime")) {
        return;
    }
    javax.xml.datatype.DatatypeFactory datatypeFactory = javax.xml.datatype.DatatypeFactory.newInstance();

    XMLGregorianCalendar x = datatypeFactory.newXMLGregorianCalendar();
    x.setYear(1975);
    x.setMonth(5);
    x.setDay(5);
    x.setHour(12);
    x.setMinute(30);
    x.setSecond(15);
    XMLGregorianCalendar yOrig = datatypeFactory.newXMLGregorianCalendar();
    yOrig.setYear(2005);
    yOrig.setMonth(4);
    yOrig.setDay(1);
    yOrig.setHour(17);
    yOrig.setMinute(59);
    yOrig.setSecond(30);

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

    XMLGregorianCalendar ret;
    if (testDocLiteral) {
        ret = docClient.testDateTime(x, y, z);
    } else if (testXMLBinding) {
        ret = xmlClient.testDateTime(x, y, z);
    } else {
        ret = rpcClient.testDateTime(x, y, z);
    }
    if (!perfTestOnly) {
        assertTrue("testDateTime(): Incorrect value for inout param", equalsDateTime(x, y.value));
        assertTrue("testDateTime(): Incorrect value for out param", equalsDateTime(yOrig, z.value));
        assertTrue("testDateTime(): Incorrect return value", equalsDateTime(x, ret));
    }
}
 
Example 6
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 7
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 8
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 9
Source File: CalendarUtilTest.java    From hsac-fitnesse-fixtures with Apache License 2.0 5 votes vote down vote up
private XMLGregorianCalendar addMonths(int amount, XMLGregorianCalendar cal) {
    cal.setMonth(6);
    cal.setDay(1);
    cal.setHour(0);
    cal.setMinute(0);
    cal.setSecond(0);
    cal.setMillisecond(0);
    return calendarUtil.addMonths(cal, amount);
}
 
Example 10
Source File: XMLGregorianCalendarAsTime.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.setHour(date.getHours());
	calendar.setMinute(date.getMinutes());
	calendar.setSecond(date.getSeconds());
	calendar.setMillisecond((int) (date.getTime() % 1000));
}
 
Example 11
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 12
Source File: ValueConverterTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Test
public void testXMLGregorianCalendarConverter() throws Exception {
    DateTimeZone tz1 = DateTimeZone.getDefault();

    MutableDateTime dateTime1 = new MutableDateTime(tz1);
    dateTime1.setDate(System.currentTimeMillis());
    Long controlValue1 = dateTime1.getMillis();

    XMLGregorianCalendar xmlCalendar1 = datatypeFactory.newXMLGregorianCalendar();
    xmlCalendar1.setYear(dateTime1.getYear());
    xmlCalendar1.setMonth(dateTime1.getMonthOfYear());
    xmlCalendar1.setDay(dateTime1.getDayOfMonth());
    xmlCalendar1.setHour(dateTime1.getHourOfDay());
    xmlCalendar1.setMinute(dateTime1.getMinuteOfHour());
    xmlCalendar1.setSecond(dateTime1.getSecondOfMinute());
    xmlCalendar1.setMillisecond(dateTime1.getMillisOfSecond());
    xmlCalendar1.setTimezone(tz1.toTimeZone().getOffset(dateTime1.getMillis()) / 60000);

    FieldDesc fieldInfo = typeDesc.getField("tranDate");

    NsObjectInputTransducer transducer = new NsObjectInputTransducer(clientService, schema, typeDesc.getTypeName());

    AvroConverter<XMLGregorianCalendar, Long> converter1 =
            (AvroConverter<XMLGregorianCalendar, Long>) transducer.getValueConverter(fieldInfo);
    assertEquals(AvroUtils._logicalTimestamp(), converter1.getSchema());
    assertEquals(XMLGregorianCalendar.class, converter1.getDatumClass());
    assertEquals(controlValue1,
            converter1.convertToAvro(xmlCalendar1));
    assertEquals(xmlCalendar1,
            converter1.convertToDatum(controlValue1));

    AvroConverter<XMLGregorianCalendar, Object> converter2 =
            (AvroConverter<XMLGregorianCalendar, Object>) transducer.getValueConverter(fieldInfo);
    assertEquals(xmlCalendar1,
            converter2.convertToDatum(new Date(controlValue1.longValue())));

    assertNull(converter1.convertToAvro(null));
}
 
Example 13
Source File: XMLGregorianCalendarToDateTimeConverter.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public XMLGregorianCalendar convertToDatum(Object timestamp) {
    if (timestamp == null) {
        return null;
    }

    long timestampMillis;
    if (timestamp instanceof Long) {
        timestampMillis = ((Long) timestamp).longValue();
    } else if (timestamp instanceof Date) {
        timestampMillis = ((Date) timestamp).getTime();
    } else {
        throw new IllegalArgumentException("Unsupported Avro timestamp value: " + timestamp);
    }

    MutableDateTime dateTime = new MutableDateTime();
    dateTime.setMillis(timestampMillis);

    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().getOffset(dateTime.getMillis()) / 60000);

    return xts;
}
 
Example 14
Source File: AbstractXMLGregorianCalendarAdapter.java    From hyperjaxb3 with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public void setHour(Calendar source, XMLGregorianCalendar target) {
	target.setHour(source.get(Calendar.HOUR_OF_DAY));
}
 
Example 15
Source File: ParseDate.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected Value evaluate(ValueFactory valueFactory, Value arg1, Value arg2) throws ValueExprEvaluationException {
	if (!(arg1 instanceof Literal) || !(arg2 instanceof Literal)) {
		throw new ValueExprEvaluationException("Both arguments must be literals");
	}
	Literal value = (Literal) arg1;
	Literal format = (Literal) arg2;

	FieldAwareGregorianCalendar cal = new FieldAwareGregorianCalendar();

	SimpleDateFormat formatter = new SimpleDateFormat(format.getLabel());
	formatter.setCalendar(cal);
	try {
		formatter.parse(value.getLabel());
	} catch (ParseException e) {
		throw new ValueExprEvaluationException(e);
	}

	XMLGregorianCalendar xmlCal = datatypeFactory.newXMLGregorianCalendar(cal);
	if (!cal.isDateSet()) {
		xmlCal.setYear(DatatypeConstants.FIELD_UNDEFINED);
		xmlCal.setMonth(DatatypeConstants.FIELD_UNDEFINED);
		xmlCal.setDay(DatatypeConstants.FIELD_UNDEFINED);
	}
	if (!cal.isTimeSet()) {
		xmlCal.setHour(DatatypeConstants.FIELD_UNDEFINED);
		xmlCal.setMinute(DatatypeConstants.FIELD_UNDEFINED);
		xmlCal.setSecond(DatatypeConstants.FIELD_UNDEFINED);
	}
	if (!cal.isMillisecondSet()) {
		xmlCal.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
	}

	String dateValue = xmlCal.toXMLFormat();
	QName dateType = xmlCal.getXMLSchemaType();
	if (!cal.isTimezoneSet()) {
		int len = dateValue.length();
		if (dateValue.endsWith("Z")) {
			dateValue = dateValue.substring(0, len - 1);
		} else if (dateValue.charAt(len - 6) == '+' || dateValue.charAt(len - 6) == '-') {
			dateValue = dateValue.substring(0, len - 6);
		}
	}

	return valueFactory.createLiteral(dateValue, XMLDatatypeUtil.qnameToURI(dateType));
}
 
Example 16
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public final void testSetHour() {

    /**
     * Hour values to test and expected result.
     */
    final int[] TEST_VALUES = {
            // setTime(H, M, S), hour override, expected result
            0, 0, 0, 0, TEST_VALUE_PASS, 0, 0, 0, 23, TEST_VALUE_PASS, 0, 0, 0, 24, TEST_VALUE_PASS,
            // creates invalid state
            0, 0, 0, DatatypeConstants.FIELD_UNDEFINED, TEST_VALUE_FAIL,
            // violates Schema Errata
            0, 0, 1, 24, TEST_VALUE_FAIL };

    // create DatatypeFactory
    DatatypeFactory datatypeFactory = null;
    try {
        datatypeFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException datatypeConfigurationException) {
        Assert.fail(datatypeConfigurationException.toString());
    }

    if (DEBUG) {
        System.err.println("DatatypeFactory created: " + datatypeFactory.toString());
    }

    // create XMLGregorianCalendar
    XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar();

    // test each value
    for (int onTestValue = 0; onTestValue < TEST_VALUES.length; onTestValue = onTestValue + 5) {

        if (DEBUG) {
            System.err.println("testing values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
                    + ", " + TEST_VALUES[onTestValue + 3] + ") expected (0=fail, 1=pass): " + TEST_VALUES[onTestValue + 4]);
        }

        try {
            // set time to known valid value
            xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]);
            // now explicitly set hour
            xmlGregorianCalendar.setHour(TEST_VALUES[onTestValue + 3]);

            if (DEBUG) {
                System.err.println("XMLGregorianCalendar created: \"" + xmlGregorianCalendar.toString() + "\"");
            }

            // was this expected to fail?
            if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_FAIL) {
                Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", "
                        + TEST_VALUES[onTestValue + 3] + ") are invalid, " + "yet it created the XMLGregorianCalendar \"" + xmlGregorianCalendar.toString()
                        + "\"");
            }
        } catch (Exception exception) {

            if (DEBUG) {
                System.err.println("Exception in creating XMLGregorianCalendar: \"" + exception.toString() + "\"");
            }

            // was this expected to succed?
            if (TEST_VALUES[onTestValue + 4] == TEST_VALUE_PASS) {
                Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2] + ", "
                        + TEST_VALUES[onTestValue + 3] + ") are valid yet it failed with \"" + exception.toString() + "\"");
            }
            // expected failure
        }
    }
}
 
Example 17
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;
}