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:DatatypeAPIUsage.java

public static void main(String[] args) {
    try {//w  w w . j a v a 2  s  .  co m
        DatatypeFactory df = DatatypeFactory.newInstance();
        // my work number in milliseconds:
        Duration myPhone = df.newDuration(9054133519l);
        Duration myLife = df.newDuration(true, 29, 2, 15, 13, 45, 0);
        int compareVal = myPhone.compare(myLife);
        switch (compareVal) {
        case DatatypeConstants.LESSER:
            System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
            break;
        case DatatypeConstants.EQUAL:
            System.out.println("The same number of milliseconds are in my phone number and my lifespan.");
            break;
        case DatatypeConstants.GREATER:
            System.out.println("There are more milliseconds in my phone number than my lifespan.");
            break;
        case DatatypeConstants.INDETERMINATE:
            System.out.println("The comparison could not be carried out.");
        }

        // create a yearMonthDuration
        Duration ymDuration = df.newDurationYearMonth("P12Y10M");
        System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());

        // create a dayTimeDuration (really this time)
        Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S");
        System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());

        // try to fool the factory!
        try {
            ymDuration = df.newDurationYearMonth("P12Y10M1D");
        } catch (IllegalArgumentException e) {
            System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
        }

        XMLGregorianCalendar xgc = df.newXMLGregorianCalendar();
        xgc.setYear(1975);
        xgc.setMonth(DatatypeConstants.AUGUST);
        xgc.setDay(11);
        xgc.setHour(6);
        xgc.setMinute(44);
        xgc.setSecond(0);
        xgc.setMillisecond(0);
        xgc.setTimezone(5);
        xgc.add(myPhone);
        System.out.println("The approximate end of the number of milliseconds in my phone number was " + xgc);

        // adding a duration to XMLGregorianCalendar
        xgc.add(myLife);
        System.out.println("Adding the duration myLife to the above calendar:" + xgc);

        // create a new XMLGregorianCalendar using the string format of xgc.
        XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());

        // should be equal-if not what happened!!
        if (xgcCopy.compare(xgc) != DatatypeConstants.EQUAL) {
            System.out.println("oooops!");
        } else {
            System.out.println("Very good: " + xgc + " is equal to " + xgcCopy);
        }
    } catch (DatatypeConfigurationException dce) {
        System.err.println("error: Datatype error occurred - " + dce.getMessage());
        dce.printStackTrace(System.err);
    }
}

From source file:Main.java

public static XMLGregorianCalendar asXMLGregorianCalendar(java.util.Date date) {
    if (date == null) {
        return null;
    } else {/*  w w w  .  java  2 s  .c  om*/
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);

        XMLGregorianCalendar xmlGregorianCalendar = df.newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));

        return xmlGregorianCalendar;
    }
}

From source file:Main.java

public static XMLGregorianCalendar convertDate(Date date) {
    try {/*from ww  w .  j  a va 2 s.co m*/
        Calendar c = new GregorianCalendar();
        c.setTime(date);
        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
        xmlGregorianCalendar.setDay(c.get(Calendar.DAY_OF_MONTH));
        xmlGregorianCalendar.setMonth(c.get(Calendar.MONTH));
        xmlGregorianCalendar.setYear(c.get(Calendar.YEAR));
        return xmlGregorianCalendar;
    } catch (DatatypeConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static XMLGregorianCalendar dateToXmlCalendar(Date date) {
    if (date != null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);//from   w  w  w .  ja v  a 2s.co  m

        try {
            XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
            xmlGregorianCalendar.setDay(calendar.get(Calendar.DAY_OF_MONTH));
            xmlGregorianCalendar.setMonth(calendar.get(Calendar.MONTH) + 1);
            xmlGregorianCalendar.setYear(calendar.get(Calendar.YEAR));
            return xmlGregorianCalendar;
        } catch (DatatypeConfigurationException e) {
            return null;
        }
    }

    return null;
}

From source file:ebay.dts.client.BulkDataExchangeActions.java

private static XMLGregorianCalendar parseDateTime(String cal) throws DatatypeConfigurationException {

    DatatypeFactory factory1 = DatatypeFactory.newInstance();
    XMLGregorianCalendar calendar1 = factory1.newXMLGregorianCalendar();
    String[] dateItems;/*from w ww. j  ava  2  s. c  om*/
    try {
        // df.parse(cal);
        if (cal.indexOf("_") == -1) {
            dateItems = cal.split("-");
            calendar1.setYear(Integer.parseInt(dateItems[0]));
            calendar1.setMonth(Integer.parseInt(dateItems[1]));
            calendar1.setDay(Integer.parseInt(dateItems[2]));
            // calendar1.setTime(00, 00, 00,000);
            calendar1.setTime(00, 00, 00);
        } else {
            String[] parts = cal.split("_");
            dateItems = parts[0].split("-");
            String[] timeItems = parts[1].split(":");
            calendar1.setYear(Integer.parseInt(dateItems[0]));
            calendar1.setMonth(Integer.parseInt(dateItems[1]));
            calendar1.setDay(Integer.parseInt(dateItems[2]));
            if (timeItems.length != 0) {
                switch (timeItems.length) {
                case 1: {
                    calendar1.setTime(Integer.parseInt(timeItems[0]), 00, 00, 000);
                    break;
                }
                case 2: {
                    calendar1.setTime(Integer.parseInt(timeItems[0]), Integer.parseInt(timeItems[1]), 00, 000);
                    break;
                }
                case 3: {
                    calendar1.setTime(Integer.parseInt(timeItems[0]), Integer.parseInt(timeItems[1]),
                            Integer.parseInt(timeItems[2]), 000);
                    break;
                }
                case 4: {
                    calendar1.setTime(Integer.parseInt(timeItems[0]), Integer.parseInt(timeItems[1]),
                            Integer.parseInt(timeItems[2]), Integer.parseInt(timeItems[3]));
                    break;
                }
                }

            }
        }

        // get here and we know the format is correct
    } catch (java.lang.NumberFormatException e) {
        logger.error("NumberFormatException caught when parse the DateTime string: " + cal);
        return null;
    }

    calendar1.setTimezone(0);
    logger.debug(calendar1.toXMLFormat());
    return calendar1;
}

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

public void testMonthFormat() throws Exception {
    log.info("*** testMonthFormat ***");
    XMLGregorianCalendar cal1 = DatatypeFactory.newInstance().newXMLGregorianCalendar();
    cal1.setMonth(GregorianCalendar.MARCH);
    String xml = cal1.toXMLFormat();
    log.debug("MAR=" + xml);

    XMLGregorianCalendar cal = DatatypeFactory.newInstance().newXMLGregorianCalendar(xml);
    assertNotNull("calendar was null", cal);
    log.info("month=" + cal.getMonth());
    assertEquals("unexpected month", GregorianCalendar.MARCH, cal.getMonth());
}

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

public void testMonthParse() 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 ww.j  a  v a2 s . com*/

    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:es.itecban.deployment.executionmanager.gui.swf.service.PlanSearchManager.java

public XMLGregorianCalendar getXMLGregorianCalendar(String date) {

    if (date == null || date.equals("")) {
        return null;
    }//from  ww  w . j a  v  a2 s .com
    XMLGregorianCalendar dateCalendar = new XMLGregorianCalendarImpl();
    String[] dateSplitted = date.split("/");
    dateCalendar.setDay(Integer.parseInt(dateSplitted[0]));
    dateCalendar.setMonth(Integer.parseInt(dateSplitted[1]));
    dateCalendar.setYear(Integer.parseInt(dateSplitted[2]));

    return dateCalendar;
}

From source file:com.headstrong.npi.raas.Utils.java

public static CmAttributeDateMonthDay getCmAttrDateMonthDay(String monthDayStr) {
    if (null != monthDayStr && !monthDayStr.trim().isEmpty()) {
        Date date = DateConversion.getDateFromMmDString(monthDayStr);
        if (null != date) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);/*from   www .ja  v  a2s  .  c  o m*/
            XMLGregorianCalendar xmlGregorianCalendar;
            try {
                xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
                xmlGregorianCalendar.setMonth(cal.get(Calendar.MONTH) + 1);
                xmlGregorianCalendar.setDay(cal.get(Calendar.DAY_OF_MONTH));
            } catch (DatatypeConfigurationException e) {
                e.printStackTrace();
                return null;
            }
            CmAttributeDateMonthDay cmAttributeDateMonthDay = new CmAttributeDateMonthDay();
            cmAttributeDateMonthDay.setValue(xmlGregorianCalendar);

            return cmAttributeDateMonthDay;
        }
    }
    return null;
}

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

protected Person createXMLPerson(DMVPerson person) {
    Person xmlPerson = new Person();
    PersonNameType name = new PersonNameType();
    name.setPersonGivenName(new PersonNameTextType());
    name.setPersonMiddleName(new PersonNameTextType());
    name.setPersonSurName(new PersonNameTextType());
    name.setPersonSuffixName(new TextType());
    xmlPerson.setPersonName(name);//from   ww  w . jav  a  2  s .c  om
    xmlPerson.setPersonBirthDate(new Date());

    xmlPerson.setSourceIDText("" + person.getId());
    xmlPerson.setId("p" + ++id);
    name.getPersonGivenName().setValue(person.getGivenName());
    name.getPersonMiddleName().setValue(person.getMiddleName());
    name.getPersonSurName().setValue(person.getSurName());
    name.getPersonSuffixName().setValue(person.getSuffixName());

    java.util.Date dob = person.getPhysicalDetails().getDob();
    if (dob != null) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(dob);
        XMLGregorianCalendar xDate = dtf.newXMLGregorianCalendar();
        xDate.setYear(cal.get(GregorianCalendar.YEAR));
        xDate.setMonth(cal.get(GregorianCalendar.MONTH) + 1);
        xDate.setDay(cal.get(GregorianCalendar.DAY_OF_MONTH));
        xmlPerson.getPersonBirthDate().setValue(xDate);
    }

    xmlPerson.setPersonPhysicalDetails(createXMLPhysicalDetails(person.getPhysicalDetails()));

    for (DMVResidence residence : person.getResidences()) {
        ResidenceType xmlResidence = createXMLResidence(residence);
        xmlPerson.getResidence().add(xmlResidence);
    }
    return xmlPerson;
}