Example usage for javax.xml.datatype XMLGregorianCalendar setMonth

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

Introduction

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

Prototype

public abstract void setMonth(int month);

Source Link

Document

Set month.

Usage

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

private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar calendar) {
    XMLGregorianCalendar xmlCal;
    try {//from  w  ww  . jav  a2s .  c  om
        xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar();
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException("Failed to create XMLGregorianCalendar", e);
    }

    xmlCal.setYear(calendar.get(Calendar.YEAR));
    xmlCal.setMonth(calendar.get(Calendar.MONTH) + 1);
    xmlCal.setDay(calendar.get(Calendar.DAY_OF_MONTH));
    xmlCal.setHour(calendar.get(Calendar.HOUR));
    xmlCal.setMinute(calendar.get(Calendar.MINUTE));
    xmlCal.setSecond(calendar.get(Calendar.SECOND));
    xmlCal.setMillisecond(calendar.get(Calendar.MILLISECOND));
    return xmlCal;
}

From source file:ejava.projects.edmv.xml.EDmvBindingTest.java

public void testCalendar() throws Exception {
    log.info("*** testCalendar ***");
    DatatypeFactory dataFactory = DatatypeFactory.newInstance();
    log.info("DataTypeFactory=" + dataFactory);
    XMLGregorianCalendar cal = dataFactory.newXMLGregorianCalendar();
    log.info("XMLGregorianCalendar=" + cal.getClass());
    cal.setMonth(GregorianCalendar.MARCH);
    String xml = cal.toXMLFormat();
    log.debug("cal=" + xml);
    dataFactory.newXMLGregorianCalendar(xml);

    cal.setTimezone(0);/* w w  w.  j ava2 s . co m*/

    Calendar jCal = Calendar.getInstance();
    jCal.clear();
    jCal.set(Calendar.MONTH, Calendar.MARCH);
    DateFormat df = DateFormat.getDateInstance();
    String dfString = df.format(jCal.getTime());
    log.debug("calendar=" + dfString);

    String format = "--01";
    try {
        XMLGregorianCalendar xCal = dataFactory.newXMLGregorianCalendar(format);
        log.info("successfully parsed:" + format + ", xCal=" + xCal.toXMLFormat());
        format = "--01--";
        xCal = dataFactory.newXMLGregorianCalendar(format);
        log.info("successfully parsed:" + format + ", xCal=" + xCal.toXMLFormat());
    } catch (Exception ex) {
        log.error("failed to parse:" + format);
        fail("failed to parse:" + format);
    }
}

From source file:org.jasig.portlet.calendar.adapter.ExchangeCalendarAdapter.java

/**
 * Get an XMLGregorianCalendar for the specified date.
 *
 * @param date//from w w  w  .ja  v  a 2 s. co  m
 * @return
 * @throws DatatypeConfigurationException
 */
protected XMLGregorianCalendar getXmlDate(DateTime date) throws DatatypeConfigurationException {
    // construct an XMLGregorianCalendar
    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = datatypeFactory.newXMLGregorianCalendar();
    start.setYear(date.getYear());
    start.setMonth(date.getMonthOfYear());
    start.setTime(date.getHourOfDay(), date.getMinuteOfHour(), date.getSecondOfMinute(),
            date.getMillisOfSecond());
    start.setDay(date.getDayOfMonth());
    return start;
}

From source file:ejava.projects.edmv.bl.DataGen.java

protected ResidenceType createXMLResidence(DMVResidence residence) {
    ResidenceType xmlResidence = new ResidenceType();
    xmlResidence.setResidenceStartDate(new Date());
    xmlResidence.setResidenceEndDate(new Date());

    GregorianCalendar gDate = new GregorianCalendar();
    XMLGregorianCalendar xDate = dtf.newXMLGregorianCalendar();

    if (residence.getStartDate() != null) {
        gDate.setTime(residence.getStartDate());
        xDate.setYear(gDate.get(GregorianCalendar.YEAR));
        xDate.setMonth(gDate.get(GregorianCalendar.MONTH) + 1);
        xDate.setDay(gDate.get(GregorianCalendar.DAY_OF_MONTH));
        xmlResidence.getResidenceStartDate().setValue(xDate);
    }/*www .  jav a 2  s . c  om*/

    if (residence.getEndDate() != null) {
        gDate.setTime(residence.getEndDate());
        xDate.setYear(gDate.get(GregorianCalendar.YEAR));
        xDate.setMonth(gDate.get(GregorianCalendar.MONTH) + 1);
        xDate.setDay(gDate.get(GregorianCalendar.DAY_OF_MONTH));
        xmlResidence.getResidenceEndDate().setValue(xDate);
    }

    if (residence.getLocation() != null) {
        AddressType address = new AddressType();
        address.setLocationStreet(new StreetType());
        address.getLocationStreet().setStreetNumberText(new TextType());
        address.getLocationStreet().setStreetName(new TextType());
        address.setLocationCityName(new TextType());
        address.setLocationStateCodeUSPostalService(new USStateCodeType());
        address.setLocationPostalCodeID(new IDType());
        address.getLocationPostalCodeID().setID(new TextType());

        address.getLocationStreet().getStreetNumberText()
                .setValue(residence.getLocation().getStreetNumber().trim());
        address.getLocationStreet().getStreetName().setValue(residence.getLocation().getStreetName().trim());
        address.getLocationCityName().setValue(residence.getLocation().getCityName().trim());
        address.getLocationStateCodeUSPostalService().setValue(residence.getLocation().getState().trim());
        address.getLocationPostalCodeID().getID().setValue(residence.getLocation().getZip().trim());

        xmlResidence.setLocationAddress(address);
    }

    return xmlResidence;
}

From source file:gov.nih.nci.cabig.caaers.service.migrator.StudyConverter.java

private XMLGregorianCalendar convertCalendar2XmlGregorianCalendar(Calendar c) throws Exception {
    XMLGregorianCalendar xml = DatatypeFactory.newInstance().newXMLGregorianCalendar();
    xml.setYear(c.get(Calendar.YEAR));
    xml.setMonth(c.get(Calendar.MONTH) + 1);
    xml.setDay(c.get(Calendar.DAY_OF_MONTH));
    return xml;//  w w w.  j av  a2 s .c  o  m
}

From source file:org.jasig.portlet.calendar.adapter.ExchangeCalendarAdapterTest.java

@Test
public void testGetInternalEvent() throws DatatypeConfigurationException {
    com.microsoft.exchange.types.CalendarEvent msEvent = new com.microsoft.exchange.types.CalendarEvent();

    // set the test event start time to 4AM on November 1, 2010
    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    XMLGregorianCalendar start = datatypeFactory.newXMLGregorianCalendar();
    start.setYear(2010);// w ww.  j  a  va2s  .  c o  m
    start.setMonth(11);
    start.setDay(1);
    start.setTime(4, 0, 0, 0);
    msEvent.setStartTime(start);

    // set the test event end time to 5AM on November 1, 2010
    XMLGregorianCalendar end = datatypeFactory.newXMLGregorianCalendar();
    end.setYear(2010);
    end.setMonth(12);
    end.setDay(1);
    end.setTime(5, 0, 0, 0);
    msEvent.setEndTime(end);

    // set the event tname and location
    CalendarEventDetails details = new CalendarEventDetails();
    details.setSubject("Naptime");
    details.setLocation("My house");
    msEvent.setCalendarEventDetails(details);

    // transform the Microsoft calendar event into a calendar portlet event
    VEvent event = adapter.getInternalEvent(3, msEvent);

    // ensure the calendar id, summary, and location are all set correctly
    assertEquals("Naptime", event.getSummary().getValue());
    assertEquals("My house", event.getLocation().getValue());

    // check the start time
    Calendar cal = Calendar.getInstance(java.util.TimeZone.getTimeZone("UTC"));
    cal.setTimeInMillis(event.getStartDate().getDate().getTime());
    assertEquals(4, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(java.util.TimeZone.getTimeZone("UTC"), cal.getTimeZone());
    assertTrue(event.getStartDate().isUtc());
    assertNull(event.getStartDate().getTimeZone());

    // check the end time
    cal.setTimeInMillis(event.getEndDate().getDate().getTime());
    assertEquals(5, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(java.util.TimeZone.getTimeZone("UTC"), cal.getTimeZone());
    assertTrue(event.getEndDate().isUtc());
    assertNull(event.getEndDate().getTimeZone());

}

From source file:org.openanzo.test.client.TestDateTime.java

/**
 * Test the conversion of javax.xml.datatype.XMLGregorianCalendar objects in the Anzo.java API into various XML Schema-types RDF literals. The test will add
 * statements using javax.xml.datatype.XMLGregorianCalendar objects and verify that when those statements are retrieved, the expected lexical value,
 * datatype, etc. are correct./*w w  w.  j  a  v a  2 s  . co  m*/
 * 
 * @throws Exception
 */
public void testXMLGregorianCalendarBecomesXsdTypedLiterals() throws Exception {

    AnzoClient client = null;
    try {
        client = new AnzoClient(getDefaultClientConfiguration());
        client.connect();
        client.reset(loadStatements("initialize.trig"), null);
        ClientGraph graph = client.getReplicaGraph(GRAPH_URI);

        DatatypeFactory df = DatatypeFactory.newInstance();

        // xsd:dateTime with time zone.
        // With time zone, a literal with a time zone is represented as a java.util.Calendar when it is retrieved even if the input is an XMLGregorianCalendar. 
        XMLGregorianCalendar cal = df.newXMLGregorianCalendar(2008, DatatypeConstants.JULY, 11, 16, 48, 32, 357,
                9 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2008-07-11T16:48:32.357+09:00",
                XMLSchema.DATETIME);
        // Without time zone
        cal = df.newXMLGregorianCalendar(2008, DatatypeConstants.JULY, 12, 16, 48, 32, 357,
                DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2008-07-12T16:48:32.357", XMLSchema.DATETIME);

        // xsd:date
        // Without time zone
        cal = df.newXMLGregorianCalendarDate(2008, DatatypeConstants.JULY, 12,
                DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2008-07-12", XMLSchema.DATE);
        // With time zone
        cal = df.newXMLGregorianCalendarDate(2008, DatatypeConstants.JULY, 11, -8 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2008-07-11-08:00", XMLSchema.DATE);

        // xsd:time
        // Without time zone
        cal = df.newXMLGregorianCalendarTime(16, 48, 32, 357, DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "16:48:32.357", XMLSchema.TIME);
        // With time zone
        cal = df.newXMLGregorianCalendarTime(16, 48, 32, 357, 4 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "16:48:32.357+04:00", XMLSchema.TIME);

        // xsd:gYearMonth
        // With time zone
        cal = df.newXMLGregorianCalendarDate(2008, DatatypeConstants.JULY, DatatypeConstants.FIELD_UNDEFINED,
                -4 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2008-07-04:00", XMLSchema.GYEARMONTH);
        // Without time zone
        cal = df.newXMLGregorianCalendarDate(2008, DatatypeConstants.AUGUST, DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2008-08", XMLSchema.GYEARMONTH);

        // xsd:gYear
        // With time zone
        cal = df.newXMLGregorianCalendarDate(2009, DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED, -6 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2009-06:00", XMLSchema.GYEAR);
        // Without time zone
        cal = df.newXMLGregorianCalendarDate(2010, DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "2010", XMLSchema.GYEAR);

        // xsd:gMonthDay
        // With time zone
        cal = df.newXMLGregorianCalendarDate(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.JULY, 15,
                -6 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "--07-15-06:00", XMLSchema.GMONTHDAY);
        // Without time zone
        cal = df.newXMLGregorianCalendarDate(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.JULY, 16,
                DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "--07-16", XMLSchema.GMONTHDAY);

        // xsd:gMonth
        // With time zone
        cal = df.newXMLGregorianCalendarDate(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.AUGUST,
                DatatypeConstants.FIELD_UNDEFINED, -6 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "--08-06:00", XMLSchema.GMONTH);
        // Without time zone
        cal = df.newXMLGregorianCalendarDate(DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.SEPTEMBER,
                DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "--09", XMLSchema.GMONTH);

        // xsd:gDay
        // With time zone
        cal = df.newXMLGregorianCalendarDate(DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED, 15, -6 * 60);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "---15-06:00", XMLSchema.GDAY);
        // Without time zone
        cal = df.newXMLGregorianCalendarDate(DatatypeConstants.FIELD_UNDEFINED,
                DatatypeConstants.FIELD_UNDEFINED, 16, DatatypeConstants.FIELD_UNDEFINED);
        addAndRetrieveNativeLiteral(client, graph, cal, cal, "---16", XMLSchema.GDAY);

        // Invalid XMLGregorianCalendar objects
        // Incomplete data. No XML Schema built-in type allows just an hour. We expect an exception.
        cal = df.newXMLGregorianCalendar();
        cal.setHour(13);
        try {
            Constants.valueFactory.createTypedLiteral(cal);
            fail("Should not get here since previous statement should throw exception.");
        } catch (AnzoRuntimeException e) {
            log.debug("Expected exception.");
        }

        // An XMLGregorianCalendar that has invalid data (February 31st) will go into the
        // system but will not be able to be parsed back into an XMLGregorianCalendar on retrieval.
        cal = df.newXMLGregorianCalendar();
        cal.setDay(31);
        cal.setMonth(DatatypeConstants.FEBRUARY);
        addAndRetrieveNativeLiteral(client, graph, cal, null, "--02-31", XMLSchema.GMONTHDAY);

    } finally {
        if (client != null) {
            client.close();
        }
    }
}

From source file:org.orcid.persistence.adapter.impl.Jpa2JaxbAdapterImpl.java

private XMLGregorianCalendar toXMLGregorianCalendarWithoutTime(Date date) {
    if (date != null) {
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(date);/* w  w  w . j ava2s .c o  m*/
        XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(c.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(c.get(Calendar.MONTH) + 1);
        xmlGregorianCalendar.setYear(c.get(Calendar.YEAR));
        return xmlGregorianCalendar;
    } else {
        return null;
    }
}

From source file:org.talend.components.netsuite.client.model.search.SearchDateFieldAdapter.java

protected XMLGregorianCalendar convertDateTime(String input) {
    String valueToParse = input;/* w  w w .  j  ava2 s  .  com*/
    String dateTimeFormatPattern = dateFormatPattern + " " + timeFormatPattern;
    if (input.length() == dateFormatPattern.length()) {
        dateTimeFormatPattern = dateFormatPattern;
    } else if (input.length() == timeFormatPattern.length()) {
        DateTime dateTime = new DateTime();
        DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(dateFormatPattern);
        valueToParse = dateFormatter.print(dateTime) + " " + input;
    }

    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dateTimeFormatPattern);

    DateTime dateTime;
    try {
        dateTime = dateTimeFormatter.parseDateTime(valueToParse);
    } catch (IllegalArgumentException e) {
        throw new NetSuiteException(new NetSuiteErrorCode(NetSuiteErrorCode.CLIENT_ERROR),
                NetSuiteRuntimeI18n.MESSAGES.getMessage("error.searchDateField.invalidDateTimeFormat",
                        valueToParse));
    }

    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;
}