Example usage for javax.xml.datatype DatatypeConstants YEARS

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

Introduction

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

Prototype

Field YEARS

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

Click Source Link

Document

A constant that represents the years 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

/**
 * 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//from   w  ww . ja  va 2 s.c o m
 */
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);
}