Example usage for javax.xml.datatype Duration add

List of usage examples for javax.xml.datatype Duration add

Introduction

In this page you can find the example usage for javax.xml.datatype Duration add.

Prototype

public abstract Duration add(final Duration rhs);

Source Link

Document

Computes a new duration whose value is this+rhs .

Usage

From source file:Main.java

/**
 * Add two positive Duration objects.//from  ww w  .  j av  a2  s .  co  m
 * @param d1 The first Duration.
 * @param d2 The second Duration.
 * @return The sum of the two durations.
 */
private static Duration addPositiveDurations(Duration d1, Duration d2) {
    BigDecimal s1 = fractionalSeconds(d1);
    BigDecimal s2 = fractionalSeconds(d2);
    BigDecimal extraSeconds = s1.add(s2);

    Duration strip1 = stripFractionalSeconds(d1);
    Duration strip2 = stripFractionalSeconds(d2);

    Duration stripResult = strip1.add(strip2);

    if (extraSeconds.compareTo(BigDecimal.ONE) >= 0) {
        stripResult = stripResult.add(DURATION_1_SECOND);
        extraSeconds = extraSeconds.subtract(BigDecimal.ONE);
    }

    BigDecimal properSeconds = BigDecimal.valueOf(stripResult.getSeconds()).add(extraSeconds);

    return FACTORY.newDuration(true, BigInteger.valueOf(stripResult.getYears()),
            BigInteger.valueOf(stripResult.getMonths()), BigInteger.valueOf(stripResult.getDays()),
            BigInteger.valueOf(stripResult.getHours()), BigInteger.valueOf(stripResult.getMinutes()),
            properSeconds);
}