public static String createPeriodString(final ReadablePeriod period) { String periodStr = "n"; if (period == null) { return "n"; } if (period.get(DurationFieldType.months()) > 0) { final int months = period.get(DurationFieldType.months()); periodStr = months + "M"; } else if (period.get(DurationFieldType.weeks()) > 0) { final int weeks = period.get(DurationFieldType.weeks()); periodStr = weeks + "w"; } else if (period.get(DurationFieldType.days()) > 0) { final int days = period.get(DurationFieldType.days()); periodStr = days + "d"; } else if (period.get(DurationFieldType.hours()) > 0) { final int hours = period.get(DurationFieldType.hours()); periodStr = hours + "h"; } else if (period.get(DurationFieldType.minutes()) > 0) { final int minutes = period.get(DurationFieldType.minutes()); periodStr = minutes + "m"; } else if (period.get(DurationFieldType.seconds()) > 0) { final int seconds = period.get(DurationFieldType.seconds()); periodStr = seconds + "s"; } return periodStr; }
} else { MutablePeriod p = new MutablePeriod(); for (int i = 0; i < period.size(); i++) { int oldVal = period.getValue(i); DurationFieldType field = period.getFieldType(i); int remainder = oldVal % m; p.add(field, oldVal - remainder); int standardUnit = 1; if (DurationFieldType.centuries().equals(field)) { f = DurationFieldType.years(); standardUnit = 100; } else if (DurationFieldType.years().equals(field)) { f = DurationFieldType.months(); standardUnit = 12;
/** * Compares this object with the specified object for equality based on the * value of each field. All ReadablePeriod instances are accepted, but only * those with a matching <code>PeriodType</code> can return true. * * @param period a readable period to check against * @return true if all the field values are equal, false if * not or the period is null or of an incorrect type */ public boolean equals(Object period) { if (this == period) { return true; } if (period instanceof ReadablePeriod == false) { return false; } ReadablePeriod other = (ReadablePeriod) period; return (other.getPeriodType() == getPeriodType() && other.getValue(0) == getValue()); }
/** * Adds the period to the instant, specifying the number of times to add. * * @param period the period to add, null means add nothing * @param instant the instant to add to * @param scalar the number of times to add * @return the updated instant */ public long add(ReadablePeriod period, long instant, int scalar) { if (scalar != 0 && period != null) { for (int i = 0, isize = period.size(); i < isize; i++) { long value = period.getValue(i); // use long to allow for multiplication (fits OK) if (value != 0) { instant = period.getFieldType(i).getField(this).add(instant, value * scalar); } } } return instant; }
/** * 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; }
return this; int[] values = getValues(); // cloned getPeriodType().addIndexedField(this, PeriodType.YEAR_INDEX, values, period.get(DurationFieldType.YEARS_TYPE)); getPeriodType().addIndexedField(this, PeriodType.MONTH_INDEX, values, period.get(DurationFieldType.MONTHS_TYPE)); getPeriodType().addIndexedField(this, PeriodType.WEEK_INDEX, values, period.get(DurationFieldType.WEEKS_TYPE)); getPeriodType().addIndexedField(this, PeriodType.DAY_INDEX, values, period.get(DurationFieldType.DAYS_TYPE)); getPeriodType().addIndexedField(this, PeriodType.HOUR_INDEX, values, period.get(DurationFieldType.HOURS_TYPE)); getPeriodType().addIndexedField(this, PeriodType.MINUTE_INDEX, values, period.get(DurationFieldType.MINUTES_TYPE)); getPeriodType().addIndexedField(this, PeriodType.SECOND_INDEX, values, period.get(DurationFieldType.SECONDS_TYPE)); getPeriodType().addIndexedField(this, PeriodType.MILLI_INDEX, values, period.get(DurationFieldType.MILLIS_TYPE)); return new Period(values, getPeriodType());
/** * Gets a copy of this Partial with the specified period added. * <p> * If the addition is zero, then <code>this</code> is returned. * Fields in the period that aren't present in the partial are ignored. * <p> * This method is typically used to add multiple copies of complex * period instances. Adding one field is best achieved using the method * {@link #withFieldAdded(DurationFieldType, int)}. * * @param period the period to add to this one, null means zero * @param scalar the amount of times to add, such as -1 to subtract once * @return a copy of this instance with the period added * @throws ArithmeticException if the new datetime exceeds the capacity */ public Partial withPeriodAdded(ReadablePeriod period, int scalar) { if (period == null || scalar == 0) { return this; } int[] newValues = getValues(); for (int i = 0; i < period.size(); i++) { DurationFieldType fieldType = period.getFieldType(i); int index = indexOf(fieldType); if (index >= 0) { newValues = getField(index).add(this, index, newValues, FieldUtils.safeMultiply(period.getValue(i), scalar)); } } return new Partial(this, newValues); }
/** * Gets the values of a period from an interval. * * @param period the period instant to use * @param startInstant the start instant of an interval to query * @param endInstant the start instant of an interval to query * @return the values of the period extracted from the interval */ public int[] get(ReadablePeriod period, long startInstant, long endInstant) { int size = period.size(); int[] values = new int[size]; if (startInstant != endInstant) { for (int i = 0; i < size; i++) { DurationField field = period.getFieldType(i).getField(this); int value = field.getDifference(endInstant, startInstant); if (value != 0) { startInstant = field.add(startInstant, value); } values[i] = value; } } return values; }
for (int i = 0; i < period.size(); i++) { int value = period.getValue(i); if (value != 0) { DurationField field = period.getFieldType(i).getField(iso); if (field.isPrecise() == false) { throw new IllegalArgumentException( "Cannot convert period to duration as " + field.getName() + " is not precise in the period " + period); duration = FieldUtils.safeAdd(duration, FieldUtils.safeMultiply(field.getUnitMillis(), value)); return FieldUtils.safeToInt(duration / millisPerUnit);
final double secs = pgi.getSeconds(); return new Period(years, months, 0, days, hours, mins, (int)secs, 0).toStandardDuration(); final ReadablePeriod period = ((ReadableDuration)value).toPeriod(); final int years = period.get(DurationFieldType.years()); final int months = period.get(DurationFieldType.months()); final int days = period.get(DurationFieldType.days()); final int hours = period.get(DurationFieldType.hours()); final int mins = period.get(DurationFieldType.minutes()); final int secs = period.get(DurationFieldType.seconds());
/** * Adds 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[] addPeriodInto(int[] values, ReadablePeriod period) { for (int i = 0, isize = period.size(); i < isize; i++) { DurationFieldType type = period.getFieldType(i); int value = period.getValue(i); if (value != 0) { int index = indexOf(type); if (index == -1) { throw new IllegalArgumentException( "Period does not support field '" + type.getName() + "'"); } else { values[index] = FieldUtils.safeAdd(getValue(index), value); } } } return values; }
for (int i = 0; i < period.size(); i++) { DurationFieldType fieldType = period.getFieldType(i); int index = indexOf(fieldType); if (index >= 0) { newValues = getField(index).addWrapPartial(this, index, newValues, FieldUtils.safeMultiply(period.getValue(i), scalar));
@Override public Duration resolve(Time refTime, int flags) { Instant instant = (refTime != null) ? refTime.getJodaTimeInstant() : null; if (instant != null) { if ((flags & DUR_RESOLVE_FROM_AS_REF) != 0) { return new DurationWithMillis(this, period.toPeriod().toDurationFrom(instant)); } else if ((flags & DUR_RESOLVE_TO_AS_REF) != 0) { return new DurationWithMillis(this, period.toPeriod().toDurationTo(instant)); } } return this; }
@Override public Duration multiplyBy(int m) { if (m == 1 || period == null) { return this; } else { MutablePeriod p = period.toMutablePeriod(); for (int i = 0; i < period.size(); i++) { p.setValue(i, period.getValue(i) * m); } return new DurationWithFields(p); } }
@Override public org.joda.time.Duration getJodaTimeDuration() { return (period != null) ? period.toPeriod().toDurationFrom(JodaTimeUtils.INSTANT_ZERO) : null; }
/** * Selects a suitable period type for the given object. * * @param object the object to examine, must not be null * @return the period type from the readable duration * @throws NullPointerException if the object is null * @throws ClassCastException if the object is an invalid type */ public PeriodType getPeriodType(Object object) { ReadablePeriod period = (ReadablePeriod) object; return period.getPeriodType(); }
/** * Gets the indexed field part of the period. * * @param period the period to query * @param index the index to use * @return the value of the field, zero if unsupported */ int getIndexedField(ReadablePeriod period, int index) { int realIndex = iIndices[index]; return (realIndex == -1 ? 0 : period.getValue(realIndex)); }