Example usage for javax.xml.datatype XMLGregorianCalendar getMonth

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

Introduction

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

Prototype

public abstract int getMonth();

Source Link

Document

Returns the month of this calendar or DatatypeConstants#FIELD_UNDEFINED .

Usage

From source file:Main.java

public static Date convertDate(XMLGregorianCalendar date) {
    return new GregorianCalendar(date.getYear(), date.getMonth(), date.getDay()).getTime();
}

From source file:Main.java

public static String toStr(XMLGregorianCalendar xmlGregorianCalendar) {
    // 1970-01-01  => hour minute secend timezone :-2147483648
    String result = "";
    result += xmlGregorianCalendar.getYear();
    result += "-";
    result += zeroize(xmlGregorianCalendar.getMonth());
    result += "-";
    result += zeroize(xmlGregorianCalendar.getDay());
    result += "T";
    result += zeroize(xmlGregorianCalendar.getHour());
    result += ":";
    result += zeroize(xmlGregorianCalendar.getMinute());
    result += ":";
    result += zeroize(xmlGregorianCalendar.getSecond());
    return result;
    //       return new Date(xmlGregorianCalendar.getMillisecond());
}

From source file:Main.java

/**
 * Converts from an XMLGregorianCalendar to a LocalDateTime i.e. represents a date and time without timezone
 * inform.ation/*from   ww w  .j av  a 2 s. co m*/
 * 
 * @param calendar XMLGregorianCalendar object.
 * @return The calendar converted to a LocalDateTime.
 */
public static LocalDateTime convertToLocalDateTime(XMLGregorianCalendar calendar) {
    if (calendar == null) {
        return null;
    }
    int year = calendar.getYear() > 0 ? calendar.getYear() : 0;
    int hour = calendar.getHour() > 0 ? calendar.getHour() : 0;
    int minute = calendar.getMinute() > 0 ? calendar.getMinute() : 0;
    int second = calendar.getSecond() > 0 ? calendar.getSecond() : 0;
    int millisecond = calendar.getMillisecond() > 0 ? calendar.getMillisecond() : 0;

    return new LocalDateTime(year, calendar.getMonth(), calendar.getDay(), hour, minute, second, millisecond);
}

From source file:Main.java

public static DateTime xmlToDateTime(final XMLGregorianCalendar xmlCal) {
    DateTime result = null;/*from  www  .ja  v  a  2s . com*/
    if (xmlCal != null) {
        int hourOfDay = xmlCal.getHour();
        int minuteOfHour = xmlCal.getMinute();
        int secondOfMinute = xmlCal.getSecond();
        int milliOfSecond = xmlCal.getMillisecond();

        if (hourOfDay < 0) {
            hourOfDay = 0;
        }
        if (minuteOfHour < 0) {
            minuteOfHour = 0;
        }
        if (secondOfMinute < 0) {
            secondOfMinute = 0;
        }
        if (milliOfSecond < 0) {
            milliOfSecond = 0;
        }
        result = new DateTime(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay(), hourOfDay, minuteOfHour,
                secondOfMinute, milliOfSecond);
    }
    return result;
}

From source file:Main.java

/**
 * Converts an XMLGregorianCalendar to a Date.
 *
 * @param xmlDate/*from   w ww  . jav  a 2s  .c o  m*/
 *            XMLGregorianCalendar to convert.
 * @return corresponding date object.
 */
public static Date getDate(final XMLGregorianCalendar xmlDate) {
    // TODO: is this equivalent to getDate(String) processing above??

    // start with UTC, i.e. no daylight savings time.
    TimeZone timezone = TimeZone.getTimeZone("GMT");

    // adjust timezone to match xmldate
    int offsetMinutes = xmlDate.getTimezone();
    if (offsetMinutes != DatatypeConstants.FIELD_UNDEFINED) {
        timezone.setRawOffset(
                // convert minutes to milliseconds
                offsetMinutes * 60 // seconds per minute
                        * 1000 // milliseconds per second
        );
    }

    // use calendar so parsed date will be UTC
    Calendar calendar = Calendar.getInstance(timezone);
    calendar.clear();
    calendar.set(xmlDate.getYear(),
            // xmlcalendar is 1 based, calender is 0 based
            xmlDate.getMonth() - 1, xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(),
            xmlDate.getSecond());
    Date date = calendar.getTime();
    int millis = xmlDate.getMillisecond();
    if (millis != DatatypeConstants.FIELD_UNDEFINED) {
        calendar.setTimeInMillis(calendar.getTimeInMillis() + millis);
    }

    return date;
}

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:org.jasig.portlet.calendar.adapter.ExchangeCalendarAdapterTest.java

@Test
public void testGetXmlDate() throws DatatypeConfigurationException {
    // construct a calendar representing 4:30PM on June 4, 2010
    DateTime date = new DateTime(2010, 6, 3, 16, 30, DateTimeZone.UTC);

    XMLGregorianCalendar xmlCal = adapter.getXmlDate(date);
    assertEquals(2010, xmlCal.getYear());
    assertEquals(6, xmlCal.getMonth());
    assertEquals(3, xmlCal.getDay());/*from w  ww. j  ava  2s . c  om*/
    assertEquals(16, xmlCal.getHour());
    assertEquals(30, xmlCal.getMinute());
    assertEquals(0, xmlCal.getSecond());
    assertEquals(0, xmlCal.getFractionalSecond().intValue());
}

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

public String toString(Object source) {
    XMLGregorianCalendar src = (XMLGregorianCalendar) source;
    SimpleDateFormat formatter = new SimpleDateFormat(DateConverter.DATE_FORMAT);
    formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, src.getYear());
    cal.set(Calendar.MONTH, src.getMonth() - 1);
    cal.set(Calendar.DAY_OF_MONTH, src.getDay());
    cal.set(Calendar.HOUR, src.getHour());
    cal.set(Calendar.MINUTE, src.getMinute());
    cal.set(Calendar.SECOND, src.getSecond());
    cal.set(Calendar.MILLISECOND, src.getMillisecond());
    return formatter.format(cal.getTime());
}

From source file:be.fedict.trust.xkms2.XKMSPortImpl.java

private Date getDate(XMLGregorianCalendar xmlCalendar) {

    GregorianCalendar calendar = new GregorianCalendar(xmlCalendar.getYear(), xmlCalendar.getMonth() - 1,
            xmlCalendar.getDay(), //
            xmlCalendar.getHour(), xmlCalendar.getMinute(), xmlCalendar.getSecond());
    calendar.setTimeZone(xmlCalendar.getTimeZone(0));
    return calendar.getTime();
}

From source file:eu.openminted.registry.service.generate.WorkflowOutputMetadataGenerate.java

protected ResourceCreationInfo generateResourceCreationInfo(String userId)
        throws JsonParseException, JsonMappingException, IOException {
    ResourceCreationInfo resourceCreationInfo = new ResourceCreationInfo();

    // resourceCreators.resourceCreator.relatedPerson
    List<ActorInfo> resourceCreators = new ArrayList<>();
    ActorInfo actorInfo = new ActorInfo();
    actorInfo.setActorType(ActorTypeEnum.PERSON);
    actorInfo.setRelatedPerson(generatePersonInfo(userId, false));
    resourceCreators.add(actorInfo);/*from   w w  w  . j a  v  a  2  s .c  o  m*/
    resourceCreationInfo.setResourceCreators(resourceCreators);

    // resourceCreationDate
    DateCombination creationDate = new DateCombination();
    XMLGregorianCalendar calendar = null;
    try {
        calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregory);
    } catch (DatatypeConfigurationException e) {
        e.printStackTrace();
    }
    Date date = new Date();
    date.setYear(calendar.getYear());
    date.setMonth(calendar.getMonth());
    date.setDay(calendar.getDay());

    creationDate.setDate(date);
    resourceCreationInfo.setCreationDate(creationDate);

    return resourceCreationInfo;
}