Search in sources :

Example 6 with DurationFieldType

use of org.joda.time.DurationFieldType in project joda-time by JodaOrg.

the class BasePeriod method mergePeriodInto.

/**
     * Merges the fields from another period.
     * 
     * @param values  the array of values to update
     * @param period  the period to add from, not null
     * @return the updated values
     * @throws IllegalArgumentException if an unsupported field's value is non-zero
     */
protected int[] mergePeriodInto(int[] values, ReadablePeriod period) {
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, values, value);
    }
    return values;
}
Also used : DurationFieldType(org.joda.time.DurationFieldType)

Example 7 with DurationFieldType

use of org.joda.time.DurationFieldType in project joda-time by JodaOrg.

the class BasePeriod method setPeriodInternal.

/**
     * Private method called from constructor.
     */
private void setPeriodInternal(ReadablePeriod period) {
    int[] newValues = new int[size()];
    for (int i = 0, isize = period.size(); i < isize; i++) {
        DurationFieldType type = period.getFieldType(i);
        int value = period.getValue(i);
        checkAndUpdate(type, newValues, value);
    }
    setValues(newValues);
}
Also used : DurationFieldType(org.joda.time.DurationFieldType)

Example 8 with DurationFieldType

use of org.joda.time.DurationFieldType in project h2o-3 by h2oai.

the class PrettyPrint method toAge.

public static String toAge(Date from, Date to) {
    if (from == null || to == null)
        return "N/A";
    final Period period = new Period(from.getTime(), to.getTime());
    DurationFieldType[] dtf = new ArrayList<DurationFieldType>() {

        {
            add(DurationFieldType.years());
            add(DurationFieldType.months());
            add(DurationFieldType.days());
            if (period.getYears() == 0 && period.getMonths() == 0 && period.getDays() == 0) {
                add(DurationFieldType.hours());
                add(DurationFieldType.minutes());
            }
        }
    }.toArray(new DurationFieldType[0]);
    PeriodFormatter pf = PeriodFormat.getDefault();
    return pf.print(period.normalizedStandard(PeriodType.forFields(dtf)));
}
Also used : PeriodFormatter(org.joda.time.format.PeriodFormatter) DurationFieldType(org.joda.time.DurationFieldType) Period(org.joda.time.Period)

Example 9 with DurationFieldType

use of org.joda.time.DurationFieldType in project presto by prestodb.

the class DateTimeUtils method parsePeriod.

private static Period parsePeriod(PeriodFormatter periodFormatter, String value) {
    boolean negative = value.startsWith("-");
    if (negative) {
        value = value.substring(1);
    }
    Period period = periodFormatter.parsePeriod(value);
    for (DurationFieldType type : period.getFieldTypes()) {
        checkArgument(period.get(type) >= 0, "Period field %s is negative", type);
    }
    if (negative) {
        period = period.negated();
    }
    return period;
}
Also used : DurationFieldType(org.joda.time.DurationFieldType) Period(org.joda.time.Period) ReadWritablePeriod(org.joda.time.ReadWritablePeriod) MutablePeriod(org.joda.time.MutablePeriod)

Example 10 with DurationFieldType

use of org.joda.time.DurationFieldType in project CoreNLP by stanfordnlp.

the class JodaTimeUtils method isMoreGeneral.

// Returns if df1 is more general than df2
public static boolean isMoreGeneral(DateTimeFieldType df1, DateTimeFieldType df2, Chronology chronology) {
    DurationFieldType df1DurationFieldType = df1.getDurationType();
    DurationFieldType df2DurationFieldType = df2.getDurationType();
    if (!df2DurationFieldType.equals(df1DurationFieldType)) {
        DurationField df1Unit = df1DurationFieldType.getField(chronology);
        DurationFieldType p = df2.getRangeDurationType();
        if (p != null) {
            DurationField df2Unit = df2DurationFieldType.getField(chronology);
            int cmp = df1Unit.compareTo(df2Unit);
            if (cmp > 0) {
                return true;
            }
        }
    }
    return false;
}
Also used : DurationFieldType(org.joda.time.DurationFieldType) ScaledDurationField(org.joda.time.field.ScaledDurationField)

Aggregations

DurationFieldType (org.joda.time.DurationFieldType)11 ScaledDurationField (org.joda.time.field.ScaledDurationField)4 DateTimeFieldType (org.joda.time.DateTimeFieldType)3 Period (org.joda.time.Period)3 DateTime (org.joda.time.DateTime)1 MutablePeriod (org.joda.time.MutablePeriod)1 ReadWritablePeriod (org.joda.time.ReadWritablePeriod)1 PeriodFormatter (org.joda.time.format.PeriodFormatter)1