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

The following examples show how to use javax.xml.datatype.XMLGregorianCalendar#add() . 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: XMLGregorianCalendarImpl.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 2
Source File: XMLGregorianCalendarImpl.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 3
Source File: DefaultSignavioDateType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar dateSubtractDuration(XMLGregorianCalendar date, Duration duration) {
    if (date == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) date.clone();
        clone.add(duration.negate());
        return clone;
    } catch (Exception e) {
        String message = String.format("dateSubtract(%s, %s)", date, duration);
        logError(message, e);
        return null;
    }
}
 
Example 4
Source File: XMLGregorianCalendarImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 5
Source File: DefaultDateTimeType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar dateTimeSubtractDuration(XMLGregorianCalendar xmlGregorianCalendar, Duration duration) {
    if (xmlGregorianCalendar == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) xmlGregorianCalendar.clone();
        clone.add(duration.negate());
        return clone;
    } catch (Exception e) {
        String message = String.format("dateTimeSubtract(%s, %s)", xmlGregorianCalendar, duration);
        logError(message, e);
        return null;
    }
}
 
Example 6
Source File: DefaultTimeType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar timeAddDuration(XMLGregorianCalendar time, Duration duration) {
    if (time == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) time.clone();
        clone.add(duration);
        return clone;
    } catch (Exception e) {
        String message = String.format("timeAdd(%s, %s)", time, duration);
        logError(message, e);
        return null;
    }
}
 
Example 7
Source File: XMLGregorianCalendarImpl.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 8
Source File: DefaultSignavioDateType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar dateAddDuration(XMLGregorianCalendar date, Duration duration) {
    if (date == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) date.clone();
        clone.add(duration);
        return clone;
    } catch (Exception e) {
        String message = String.format("dateAdd(%s, %s)", date, duration);
        logError(message, e);
        return null;
    }
}
 
Example 9
Source File: DefaultDateType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar dateSubtractDuration(XMLGregorianCalendar date, Duration duration) {
    if (date == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) date.clone();
        clone.add(duration.negate());
        return clone;
    } catch (Exception e) {
        String message = String.format("dateSubtract(%s, %s)", date, duration);
        logError(message, e);
        return null;
    }
}
 
Example 10
Source File: DefaultSignavioDateTimeType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar dateTimeSubtractDuration(XMLGregorianCalendar xmlGregorianCalendar, Duration duration) {
    if (xmlGregorianCalendar == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) xmlGregorianCalendar.clone();
        clone.add(duration.negate());
        return clone;
    } catch (Exception e) {
        String message = String.format("dateTimeSubtract(%s, %s)", xmlGregorianCalendar, duration);
        logError(message, e);
        return null;
    }
}
 
Example 11
Source File: XMLGregorianCalendarImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 12
Source File: XMLGregorianCalendarImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
     * <p>Normalize this instance to UTC.</p>
     *
     * <p>2000-03-04T23:00:00+03:00 normalizes to 2000-03-04T20:00:00Z</p>
     * <p>Implements W3C XML Schema Part 2, Section 3.2.7.3 (A).</p>
     */
private XMLGregorianCalendar normalizeToTimezone(int timezone) {

    int minutes = timezone;
    XMLGregorianCalendar result = (XMLGregorianCalendar) this.clone();

    // normalizing to UTC time negates the timezone offset before
    // addition.
    minutes = -minutes;
    Duration d = new DurationImpl(minutes >= 0, // isPositive
            0, //years
            0, //months
            0, //days
            0, //hours
            minutes < 0 ? -minutes : minutes, // absolute
            0  //seconds
    );
    result.add(d);

    // set to zulu UTC time.
    result.setTimezone(0);
    return result;
}
 
Example 13
Source File: DefaultSignavioTimeType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar timeAddDuration(XMLGregorianCalendar time, Duration duration) {
    if (time == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) time.clone();
        clone.add(duration);
        return clone;
    } catch (Exception e) {
        String message = String.format("timeAdd(%s, %s)", time, duration);
        logError(message, e);
        return null;
    }
}
 
Example 14
Source File: DefaultSignavioTimeType.java    From jdmn with Apache License 2.0 6 votes vote down vote up
@Override
public XMLGregorianCalendar timeSubtractDuration(XMLGregorianCalendar time, Duration duration) {
    if (time == null || duration == null) {
        return null;
    }

    try {
        XMLGregorianCalendar clone = (XMLGregorianCalendar) time.clone();
        clone.add(duration.negate());
        return clone;
    } catch (Exception e) {
        String message = String.format("timeSubtract(%s, %s)", time, duration);
        logError(message, e);
        return null;
    }
}
 
Example 15
Source File: XMLDatatypeMathUtil.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Literal operationsBetweenCalendarAndDuration(Literal calendarLit, Literal durationLit, MathOp op) {
	XMLGregorianCalendar calendar = (XMLGregorianCalendar) calendarLit.calendarValue().clone();
	Duration duration = XMLDatatypeUtil.parseDuration(durationLit.getLabel());

	try {
		switch (op) {
		case PLUS:
			// op:add-yearMonthDuration-to-dateTime and op:add-dayTimeDuration-to-dateTime and
			// op:add-yearMonthDuration-to-date and op:add-dayTimeDuration-to-date and
			// op:add-dayTimeDuration-to-time
			calendar.add(duration);
			return SimpleValueFactory.getInstance().createLiteral(calendar);
		case MINUS:
			// op:subtract-yearMonthDuration-from-dateTime and op:subtract-dayTimeDuration-from-dateTime and
			// op:subtract-yearMonthDuration-from-date and op:subtract-dayTimeDuration-from-date and
			// op:subtract-dayTimeDuration-from-time
			calendar.add(duration.negate());
			return SimpleValueFactory.getInstance().createLiteral(calendar);
		case MULTIPLY:
			throw new ValueExprEvaluationException(
					"Multiplication is not defined between xsd:duration and calendar values.");
		case DIVIDE:
			throw new ValueExprEvaluationException(
					"Division is not defined between xsd:duration and calendar values.");
		default:
			throw new IllegalArgumentException("Unknown operator: " + op);
		}
	} catch (IllegalStateException e) {
		throw new ValueExprEvaluationException(e);
	}
}
 
Example 16
Source File: DefaultSignavioDateLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public XMLGregorianCalendar dayAdd(XMLGregorianCalendar dateTime, BigDecimal daysToAdd) {
    XMLGregorianCalendar result = (XMLGregorianCalendar) dateTime.clone();
    int days = daysToAdd.intValue();
    boolean isPositive = days > 0;
    Duration duration;
    duration = DATA_TYPE_FACTORY.newDurationDayTime(
            isPositive, daysToAdd.abs().intValue(), 0, 0, 0);
    result.add(duration);
    return result;
}
 
Example 17
Source File: DefaultSignavioDateLib.java    From jdmn with Apache License 2.0 5 votes vote down vote up
public XMLGregorianCalendar yearAdd(XMLGregorianCalendar dateTime, BigDecimal yearsToAdd) {
    XMLGregorianCalendar result = (XMLGregorianCalendar) dateTime.clone();
    int months = yearsToAdd.intValue();
    boolean isPositive = months > 0;
    Duration duration;
    duration = DATA_TYPE_FACTORY.newDurationYearMonth(
            isPositive, yearsToAdd.abs().intValue(), 0);
    result.add(duration);
    return result;
}
 
Example 18
Source File: SubscriptionManagerImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public ExpirationType renew(UUID uuid, ExpirationType requestedExpiration) {
    SubscriptionTicket ticket = getDatabase().findById(uuid);
    if (ticket == null) {
        throw new UnknownSubscription();
    }
    LOG.info("[subscription=" + ticket.getUuid() + "] Requested renew expiration: "
            + requestedExpiration.getValue());
    LOG.fine("[subscription=" + ticket.getUuid() + "] Current expiration: " + ticket.getExpires().toXMLFormat());
    ExpirationType response = new ExpirationType();
    XMLGregorianCalendar grantedExpires;
    if (DurationAndDateUtil.isDuration(requestedExpiration.getValue())) {
        // duration was requested
        javax.xml.datatype.Duration requestedDuration = DurationAndDateUtil
                .parseDuration(requestedExpiration.getValue());
        javax.xml.datatype.Duration grantedDuration = requestedDuration;
        LOG.info("[subscription=" + ticket.getUuid() + "] Granted renewal duration: " + grantedDuration.toString());
        grantedExpires = getDatabase().findById(uuid)
                .getExpires();       // NOW() or current Expires() ????
        grantedExpires.add(grantedDuration);
        response.setValue(grantedDuration.toString());
    } else {
        // end-date was requested
        grantedExpires = DurationAndDateUtil.parseXMLGregorianCalendar(requestedExpiration.getValue());
        LOG.info("[subscription=" + ticket.getUuid() + "] Granted expiration: " + grantedExpires.toXMLFormat());
        response.setValue(grantedExpires.toXMLFormat());
    }
    getDatabase().findById(uuid).setExpires(grantedExpires);
    return response;
}
 
Example 19
Source File: SubscriptionManagerImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
/**
 * Decide what expiration time to grant to the subscription, if
 * the client did not specify any particular wish for subscription length.
 */
public XMLGregorianCalendar grantExpiration() {
    try { // by default, we grant an expiration time of 2 years
        DatatypeFactory factory = DatatypeFactory.newInstance();
        XMLGregorianCalendar granted = factory.newXMLGregorianCalendar(new GregorianCalendar());
        granted.add(factory.newDurationYearMonth(true, 2, 0));
        return granted;
    } catch (DatatypeConfigurationException ex) {
        throw new Error(ex);
    }
}
 
Example 20
Source File: CalendarUtil.java    From hsac-fitnesse-fixtures with Apache License 2.0 3 votes vote down vote up
/**
 * Add Months to a Gregorian Calendar.
 * 
 * @param cal The XMLGregorianCalendar source
 * @param amount The amount of months. Can be a negative Integer to
 *            substract.
 * @return A XMLGregorianCalendar with the new Date
 */
public XMLGregorianCalendar addMonths(final XMLGregorianCalendar cal, final int amount) {
    XMLGregorianCalendar to = buildXMLGregorianCalendarDate(cal);
    // Add amount of months
    to.add(addMonths(amount));
    return to;
}