Example usage for javax.xml.datatype Duration compare

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

Introduction

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

Prototype

public abstract int compare(final Duration duration);

Source Link

Document

Partial order relation comparison with this Duration instance.

Usage

From source file:DatatypeAPIUsage.java

public static void main(String[] args) {
    try {//from w  ww  .  jav a 2s  .c  om
        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

/**
 * Subtract one Duration from another, avoiding the runtime library bug that gives
 * incorrect results when using decimal seconds.
 * @param d1 The first duration/*from   w w w  . j a va 2  s.  co  m*/
 * @param d2 The second duration
 * @return The result of subtracting d2 from d1
 */
public static Duration subtract(Duration d1, Duration d2) {
    boolean sign1 = d1.getSign() >= 0;
    boolean sign2 = d2.getSign() >= 0;

    if (sign1 && sign2) {
        int comparison = d1.compare(d2);
        comparison = compare(d1, d2);
        if (comparison >= 0)
            return subtractSmallerPositiveDurationFromLargerPositiveDuration(d1, d2);
        else
            return subtractSmallerPositiveDurationFromLargerPositiveDuration(d2, d1).negate();
    }

    if (!sign1 && !sign2) {
        d1 = d1.negate();
        d2 = d2.negate();

        int comparison = d1.compare(d2);
        comparison = compare(d1, d2);
        if (comparison < 0)
            return subtractSmallerPositiveDurationFromLargerPositiveDuration(d2, d1);
        else
            return subtractSmallerPositiveDurationFromLargerPositiveDuration(d1, d2).negate();
    }

    if (sign1 && !sign2)
        return add(d1, d2.negate());

    //if( ! sign1 && sign2 )
    return add(d2, d1.negate()).negate();
}