Example usage for org.joda.time Period Period

List of usage examples for org.joda.time Period Period

Introduction

In this page you can find the example usage for org.joda.time Period Period.

Prototype

public Period(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis) 

Source Link

Document

Create a period from a set of field values using the standard set of fields.

Usage

From source file:org.apache.isis.applib.value.Date.java

License:Apache License

/**
 * Add the specified days, years and months to this date value and return a
 * new date object containing the result.
 *///  w  w  w . j a v a 2  s.c o  m
public Date add(final int years, final int months, final int days) {
    final Period add = new Period(years, months, 0, days, 0, 0, 0, 0);
    final DateTime newDate = date.plus(add);
    return new Date(newDate);
}

From source file:org.apache.isis.applib.value.DateTime.java

License:Apache License

/**
 * Add the specified time period to this date value.
 *///from  www .  ja v a 2 s  .c  om
public DateTime add(final int years, final int months, final int days, final int hours, final int minutes) {
    final Period period = new Period(years, months, 0, days, hours, minutes, 0, 0);
    final org.joda.time.DateTime dateTime = this.dateTime.plus(period);
    return new DateTime(dateTime);
}

From source file:org.brekka.stillingar.jaxb.conversion.PeriodConverter.java

License:Apache License

@Override
public Period convert(Object obj) {
    Period value;/* ww w.  j  a v a 2s.  c  o m*/
    if (obj instanceof Duration) {
        Duration duration = (Duration) obj;
        int times = duration.getSign();
        value = new Period(duration.getYears() * times, duration.getMonths() * times, 0, // no weeks
                duration.getDays() * times, duration.getHours() * times, duration.getMinutes() * times,
                duration.getSeconds() * times, 0 // No millis
        );
    } else {
        value = super.convert(obj);
    }
    return value;
}

From source file:org.brekka.stillingar.xmlbeans.conversion.PeriodConverter.java

License:Apache License

@Override
public Period convert(Object obj) {
    Period value;//from  w w  w.  jav a 2s .co m
    if (obj instanceof XmlDuration) {
        XmlDuration xDuration = (XmlDuration) obj;
        GDuration gDuration = xDuration.getGDurationValue();
        int times = gDuration.getSign();
        value = new Period(gDuration.getYear() * times, gDuration.getMonth() * times, 0,
                gDuration.getDay() * times, gDuration.getHour() * times, gDuration.getMinute() * times,
                gDuration.getSecond() * times,
                gDuration.getFraction().multiply(BigDecimal.valueOf(0L)).intValue());
    } else {
        value = super.convert(obj);
    }
    return value;
}

From source file:org.openehr.rm.datatypes.quantity.datetime.DvDuration.java

License:LGPL

/**
 * Constructs a Duration with referenceRange and accuracy
 *
 * @param referenceRanges/*  w w w  . j a v a2  s  .  com*/
 * @param accuracy
 * @param accuracyPercent
 * @param days
 * @param hours
 * @param minutes
 * @param seconds
 * @param fractionalSeconds
 */
public DvDuration(List<ReferenceRange<DvDuration>> referenceRanges, DvInterval<DvDuration> normalRange,
        double accuracy, boolean accuracyPercent, int years, int months, int weeks, int days, int hours,
        int minutes, int seconds, double fractionalSeconds) {

    super(referenceRanges, normalRange, null, accuracy, accuracyPercent, null);

    if (Math.abs(fractionalSeconds) >= 1.0) {
        throw new IllegalArgumentException("invalid fraction seconds: " + fractionalSeconds);
    }

    if (!isValidCombination(years, months, weeks, days, hours, minutes, seconds, fractionalSeconds)) {
        throw new IllegalArgumentException("invalid combination for period");
    }

    period = new Period(years, months, weeks, days, hours, minutes, seconds, (int) (fractionalSeconds * 1000));
    setValue(ISOPeriodFormat.standard().print(period).replace(".", ","));
}

From source file:org.openvpms.archetype.i18n.time.DateDurationFormatter.java

License:Open Source License

/**
 * Formats the duration between two timestamps.
 * <p/>// w w w.j  a v a  2 s .  com
 * NOTE: this currently doesn't do anything sensible for from > to. Possible solution would be to simply
 * reverse the times, and then prepend a - between each field using  the
 *
 * @param from the starting time
 * @param to   the ending time
 * @return the formatted duration
 */
protected String format(DateTime from, DateTime to) {
    int years = 0;
    int months = 0;
    int weeks = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;

    DateTime start = from;
    if (showYears) {
        years = Years.yearsBetween(start, to).getYears();
        start = start.plusYears(years);
    }
    if (showMonths) {
        months = Months.monthsBetween(start, to).getMonths();
        start = start.plusMonths(months);
    }
    if (showWeeks) {
        weeks = Weeks.weeksBetween(start, to).getWeeks();
        start = start.plusWeeks(weeks);
    }
    if (showDays) {
        days = Days.daysBetween(start, to).getDays();
        start = start.plusDays(days);
    }
    if (showHours) {
        hours = Hours.hoursBetween(start, to).getHours();
        start = start.plusHours(hours);
    }
    if (showMinutes) {
        minutes = Minutes.minutesBetween(start, to).getMinutes();
    }

    Period period = new Period(years, months, weeks, days, hours, minutes, 0, 0);
    return formatter.print(period);
}