Example usage for javax.xml.datatype XMLGregorianCalendar setYear

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

Introduction

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

Prototype

public abstract void setYear(int year);

Source Link

Document

Set year of XSD dateTime year field.

Usage

From source file:DatatypeAPIUsage.java

public static void main(String[] args) {
    try {// w  w w.  ja v a2s .c o  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. j  a  va  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  www.j  ava  2  s .  c om
        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);//  w  w  w .  j a v a  2 s .  c om

        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;/* ww w.j  av  a  2s.  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.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.  j  a  va2  s. c  o m*/
    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;
}

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

private XMLGregorianCalendar calendarToXMLGregorianCalendar(Calendar calendar) {
    XMLGregorianCalendar xmlCal;
    try {/* w w  w  . j a va2s  .  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:es.itecban.deployment.executionmanager.gui.swf.service.PlanSearchManager.java

public XMLGregorianCalendar getXMLGregorianCalendar(String date) {

    if (date == null || date.equals("")) {
        return null;
    }//w  w  w .j a v  a 2  s.  co m
    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:org.jasig.portlet.calendar.adapter.ExchangeCalendarAdapter.java

/**
 * Get an XMLGregorianCalendar for the specified date.
 *
 * @param date//from  w w w  . j  a 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);
    }/*from  w w w .j av a  2s.co  m*/

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