Example usage for javax.xml.datatype DatatypeConstants SECONDS

List of usage examples for javax.xml.datatype DatatypeConstants SECONDS

Introduction

In this page you can find the example usage for javax.xml.datatype DatatypeConstants SECONDS.

Prototype

Field SECONDS

To view the source code for javax.xml.datatype DatatypeConstants SECONDS.

Click Source Link

Document

A constant that represents the seconds field.

Usage

From source file:Main.java

private static javax.xml.datatype.Duration normaliseSeconds(javax.xml.datatype.Duration duration) {
    BigInteger years = (BigInteger) duration.getField(DatatypeConstants.YEARS);
    BigInteger months = (BigInteger) duration.getField(DatatypeConstants.MONTHS);
    BigInteger days = (BigInteger) duration.getField(DatatypeConstants.DAYS);

    BigInteger hours = (BigInteger) duration.getField(DatatypeConstants.HOURS);
    BigInteger minutes = (BigInteger) duration.getField(DatatypeConstants.MINUTES);
    BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS);

    seconds = seconds.stripTrailingZeros();

    boolean positive = duration.getSign() >= 0;

    return FACTORY.newDuration(positive, years, months, days, hours, minutes, seconds);
}

From source file:Main.java

/**
 * Return just the fractional part of the seconds component of a duration object,
 * i.e. 1:2:34.45 => 0.45// w w  w.  j  a  v  a 2  s .  c om
 * @param duration The duration object to examine.
 * @return The fractional part of the seconds field.
 */
private static BigDecimal fractionalSeconds(Duration duration) {
    BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS);

    return seconds.subtract(new BigDecimal(seconds.toBigInteger()));
}

From source file:Main.java

/**
 * Special compare method that gets around the problem in java 1.5,
 * where years and months are converted to days after arithmetic.
 * @param d1 First duration//from  w w  w  .  jav a  2s.  c  o m
 * @param d2 Second duration
 * @return -1 if d1 < d2, 0 if d1 == d2 and +1 if d1 > d2
 */
public static int compare(Duration d1, Duration d2) {
    if (d1 == null && d2 == null)
        return 0;

    if (d1 == null)
        return -1;

    if (d2 == null)
        return 1;

    boolean b1 = d1.getSign() >= 0;
    boolean b2 = d2.getSign() >= 0;

    if (!b1 && b2)
        return -1;

    if (b1 && !b2)
        return 1;

    // Now normalise in case we are running with java 1.5 runtime
    javax.xml.datatype.Duration n1 = normaliseDays(d1);
    javax.xml.datatype.Duration n2 = normaliseDays(d2);

    if (n1.getDays() < n2.getDays())
        return -1;
    if (n1.getDays() > n2.getDays())
        return 1;

    if (n1.getHours() < n2.getHours())
        return -1;
    if (n1.getHours() > n2.getHours())
        return 1;

    if (n1.getMinutes() < n2.getMinutes())
        return -1;
    if (n1.getMinutes() > n2.getMinutes())
        return 1;

    BigDecimal s1 = (BigDecimal) n1.getField(DatatypeConstants.SECONDS);
    BigDecimal s2 = (BigDecimal) n2.getField(DatatypeConstants.SECONDS);

    return s1.compareTo(s2);
}

From source file:Main.java

/**
 * Java runtime 1.5 is inconsistent with its handling of days in Duration objects.
 * @param duration A duration object to be normalised
 * @return A day-normalised duration, i.e. all years and months converted to days,
 * e.g. 1Y 3M 3D => 458 days//w  w w . jav a  2  s.  c  om
 */
private static javax.xml.datatype.Duration normaliseDays(javax.xml.datatype.Duration duration) {
    final long DAYS_PER_MONTH = 30;
    final long DAYS_PER_YEAR = 365;

    BigInteger days = (BigInteger) duration.getField(DatatypeConstants.DAYS);
    BigInteger months = (BigInteger) duration.getField(DatatypeConstants.MONTHS);
    BigInteger years = (BigInteger) duration.getField(DatatypeConstants.YEARS);

    BigInteger normalisedDays = years.multiply(BigInteger.valueOf(DAYS_PER_YEAR));
    normalisedDays = normalisedDays.add(months.multiply(BigInteger.valueOf(DAYS_PER_MONTH)));
    normalisedDays = normalisedDays.add(days);

    BigInteger hours = (BigInteger) duration.getField(DatatypeConstants.HOURS);
    BigInteger minutes = (BigInteger) duration.getField(DatatypeConstants.MINUTES);
    BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS);

    boolean positive = duration.getSign() >= 0;

    return FACTORY.newDuration(positive, BigInteger.ZERO, BigInteger.ZERO, normalisedDays, hours, minutes,
            seconds);
}