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

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#setTime() . 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: DateUtils.java    From training with MIT License 6 votes vote down vote up
public static XMLGregorianCalendar extractXmlDate(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.setTime(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
		date.setMillisecond(DatatypeConstants.FIELD_UNDEFINED);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return date;
}
 
Example 2
Source File: FATCAMetadata.java    From IDES-Data-Preparation-Java with Creative Commons Zero v1.0 Universal 5 votes vote down vote up
protected XMLGregorianCalendar genTaxYear(int year) {
	XMLGregorianCalendar taxyear = new XMLGregorianCalendarImpl(new GregorianCalendar());
	taxyear.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setTime(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setDay(DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setMonth(DatatypeConstants.FIELD_UNDEFINED);
	taxyear.setYear(year);
	return taxyear;
}
 
Example 3
Source File: XMLGregorianCalendarTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public final void testSetTime() {

    /**
     * Hour, minute, second values to test and expected result.
     */
    final int[] TEST_VALUES = { 24, 0, 0, TEST_VALUE_PASS, 24, 1, 0, TEST_VALUE_FAIL, 24, 0, 1, TEST_VALUE_FAIL, 24, DatatypeConstants.FIELD_UNDEFINED, 0,
            TEST_VALUE_FAIL, 24, 0, DatatypeConstants.FIELD_UNDEFINED, 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 + 4) {

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

        try {
            // set time
            xmlGregorianCalendar.setTime(TEST_VALUES[onTestValue], TEST_VALUES[onTestValue + 1], TEST_VALUES[onTestValue + 2]);

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

            // was this expected to fail?
            if (TEST_VALUES[onTestValue + 3] == TEST_VALUE_FAIL) {
                Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
                        + ") 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 + 3] == TEST_VALUE_PASS) {
                Assert.fail("the values: (" + TEST_VALUES[onTestValue] + ", " + TEST_VALUES[onTestValue + 1] + ", " + TEST_VALUES[onTestValue + 2]
                        + ") are valid yet it failed with \"" + exception.toString() + "\"");
            }
            // expected failure
        }
    }
}
 
Example 4
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 5
Source File: Marshalling.java    From proxymusic with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Annotate the scorePartwise tree with information about MusicXML version and, if
 * so desired, with signature composed of ProxyMusic version and date of marshalling.
 *
 * @param scorePartwise   the tree to annotate
 * @param injectSignature if true, ProxyMusic information is added
 */
private static void annotate (final ScorePartwise scorePartwise,
                              final boolean injectSignature)
        throws DatatypeConfigurationException
{
    // Predefined factory for all proxymusic elements
    ObjectFactory factory = new ObjectFactory();

    // Inject version
    scorePartwise.setVersion(ProgramId.VERSION);

    // Inject signature if so desired
    if (injectSignature) {
        // Identification
        Identification identification = scorePartwise.getIdentification();

        if (identification == null) {
            identification = factory.createIdentification();
            scorePartwise.setIdentification(identification);
        }

        // Encoding
        Encoding encoding = identification.getEncoding();

        if (encoding == null) {
            encoding = factory.createEncoding();
            identification.setEncoding(encoding);
        }

        // [Encoding]/Software (only if ProxyMusic is not already listed there)
        List<JAXBElement<?>> list = encoding.getEncodingDateOrEncoderOrSoftware();
        final String programName = ProgramId.NAME + " ";

        for (Iterator<JAXBElement<?>> it = list.iterator(); it.hasNext();) {
            JAXBElement<?> element = it.next();

            if (element.getName().getLocalPart().equals("software")) {
                Object obj = element.getValue();

                if (obj instanceof String && ((String) obj).startsWith(programName)) {
                    // Remove it
                    it.remove();

                    break;
                }
            }
        }

        list.add(
                factory.createEncodingSoftware(
                        ProgramId.NAME + " " + ProgramId.VERSION + "." + ProgramId.REVISION));

        // [Encoding]/EncodingDate (overwrite any existing date)
        for (Iterator<JAXBElement<?>> it = list.iterator(); it.hasNext();) {
            if (it.next().getName().getLocalPart().equals("encoding-date")) {
                it.remove();

                break;
            }
        }

        // Use date without time information (patch by lasconic)
        // Output:     2012-05-03
        // instead of: 2012-05-03T16:17:51.250+02:00
        XMLGregorianCalendar gc = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(
                        new GregorianCalendar());
        gc.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
        gc.setTime(
                DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED);
        list.add(factory.createEncodingEncodingDate(gc));
    }
}