Refine search
/** * Creates a new period based on another using the {@link ConverterManager}. * * @param period the period to convert * @param type which set of fields this period supports, null means use type from object * @param chrono the chronology to use, null means ISO default * @throws IllegalArgumentException if period is invalid * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected BasePeriod(Object period, PeriodType type, Chronology chrono) { super(); PeriodConverter converter = ConverterManager.getInstance().getPeriodConverter(period); type = (type == null ? converter.getPeriodType(period) : type); type = checkPeriodType(type); iType = type; if (this instanceof ReadWritablePeriod) { iValues = new int[size()]; chrono = DateTimeUtils.getChronology(chrono); converter.setInto((ReadWritablePeriod) this, period, chrono); } else { iValues = new MutablePeriod(period, type, chrono).getValues(); } }
/** * Adds a millisecond duration to this one by dividing the duration into * fields and calling {@link #add(ReadablePeriod)}. * <p> * When dividing the duration, only precise fields in the period type will be used. * For large durations, all the remaining duration will be stored in the largest * available precise field. * * @param duration the duration, in milliseconds * @throws ArithmeticException if the addition exceeds the capacity of the period */ public void add(long duration) { add(new Period(duration, getPeriodType())); }
/** * Parses a period from the given text, returning a new Period. * * @param text text to parse * @return parsed value in a Period object * @throws IllegalArgumentException if any field is out of range */ public Period parsePeriod(String text) { checkParser(); return parseMutablePeriod(text).toPeriod(); }
/** * Get this object as a <code>MutablePeriod</code>. * <p> * This will always return a new <code>MutablePeriod</code> with the same fields. * The period will use <code>PeriodType.standard()</code>. * * @return a MutablePeriod using the same field set and values */ public MutablePeriod toMutablePeriod() { MutablePeriod period = new MutablePeriod(); period.add(this); return period; }
/** * Adds an interval to this one by dividing the interval into * fields and calling {@link #add(ReadablePeriod)}. * * @param interval the interval to add, null means add nothing * @throws ArithmeticException if the addition exceeds the capacity of the period */ public void add(ReadableInterval interval) { if (interval != null) { add(interval.toPeriod(getPeriodType())); } }
/** * Adds to each field of this period. * * @param years amount of years to add to this period, which must be zero if unsupported * @param months amount of months to add to this period, which must be zero if unsupported * @param weeks amount of weeks to add to this period, which must be zero if unsupported * @param days amount of days to add to this period, which must be zero if unsupported * @param hours amount of hours to add to this period, which must be zero if unsupported * @param minutes amount of minutes to add to this period, which must be zero if unsupported * @param seconds amount of seconds to add to this period, which must be zero if unsupported * @param millis amount of milliseconds to add to this period, which must be zero if unsupported * @throws IllegalArgumentException if the period being added contains a field * not supported by this period * @throws ArithmeticException if the addition exceeds the capacity of the period */ public void add(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis) { setPeriod( FieldUtils.safeAdd(getYears(), years), FieldUtils.safeAdd(getMonths(), months), FieldUtils.safeAdd(getWeeks(), weeks), FieldUtils.safeAdd(getDays(), days), FieldUtils.safeAdd(getHours(), hours), FieldUtils.safeAdd(getMinutes(), minutes), FieldUtils.safeAdd(getSeconds(), seconds), FieldUtils.safeAdd(getMillis(), millis) ); }
/** * Helper method to negate a copy of period * * @param mPeriod * @return a negated copy of period */ static Period negatePeriod(Period period) { MutablePeriod mPeriod = period.toMutablePeriod(); mPeriod.setYears(-mPeriod.getYears()); mPeriod.setMonths(-mPeriod.getMonths()); mPeriod.setWeeks(-mPeriod.getWeeks()); mPeriod.setDays(-mPeriod.getDays()); mPeriod.setHours(-mPeriod.getHours()); mPeriod.setMinutes(-mPeriod.getMinutes()); mPeriod.setSeconds(-mPeriod.getSeconds()); mPeriod.setMillis(-mPeriod.getMillis()); return mPeriod.toPeriod(); }
return this; } 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); if (remainder != 0) { DurationFieldType f; throw new UnsupportedOperationException("Unsupported duration type: " + field + " when dividing"); p.add(f, standardUnit * remainder); for (int i = 0; i < p.size(); i++) { p.setValue(i, p.getValue(i) / m);
/** * Get this object as a <code>MutablePeriod</code>. * <p> * This will always return a new <code>MutablePeriod</code> with the same fields. * * @return a MutablePeriod using the same field set and values */ public MutablePeriod toMutablePeriod() { return new MutablePeriod(this); }
@Override public Period load(ParseableDurationForLocale parseableDurationForLocale) throws Exception { MutablePeriod period = new MutablePeriod(); PERIOD_PARSER.parseInto(period, parseableDurationForLocale.duration, 0, parseableDurationForLocale.locale); return period.toPeriod(); } });
continue; MutablePeriod p = new MutablePeriod(); p.setYears(medication.getIntValue(DURATION_YEARS)); p.setMonths(medication.getIntValue(DURATION_MONTHS)); result.add(new Medication(medication.getStringValue(Medication.NAME), medication.getStringValue(Medication.GENERIC_NAME), medication.getStringValue(Medication.DOSE), medication.getStringValue(Medication.FREQUENCY), p.toPeriod(), medication.getStringValue(Medication.EFFECT), medication.getLargeStringValue(Medication.NOTES)));
MutablePeriod mp = new MutablePeriod(); parser.parseInto(mp, time, 0, Locale.getDefault()); int hours = mp.getMinutes()/ 60; int minutes = mp.getMinutes() % 60; mp.setHours(mp.getHours()+hours); mp.setMinutes(minutes);
/** * Clears the period, setting all values back to zero. */ public void clear() { super.setValues(new int[size()]); }
MutablePeriod period = Period.ZERO.toMutablePeriod(); if ( type == null ) throw new DatePeriodException( "Invalid period type: " + matcher.group( 2 ) ); period.add( type.getDurationFieldType(), amount * modifier ); return period.toPeriod();
MutablePeriod period = new MutablePeriod(Math.abs(duration.getMillis()), periodType); if (period.isSupported(DurationFieldType.years())) { periodFormatterBuilder.appendYears().appendSuffix( RenderUtils.getResourceString(enumerationBundle, CLASS_NAME + DurationFieldType.years().getName() + SHORT)); if (period.isSupported(DurationFieldType.months())) { periodFormatterBuilder.appendMonths().appendSuffix( RenderUtils.getResourceString(enumerationBundle, CLASS_NAME + DurationFieldType.months().getName() + SHORT)); if (period.isSupported(DurationFieldType.weeks())) { periodFormatterBuilder.appendWeeks().appendSuffix( RenderUtils.getResourceString(enumerationBundle, CLASS_NAME + DurationFieldType.weeks().getName() + SHORT)); if (period.isSupported(DurationFieldType.days())) { periodFormatterBuilder.appendDays().appendSuffix( RenderUtils.getResourceString(enumerationBundle, CLASS_NAME + DurationFieldType.days().getName() + SHORT)); if (period.isSupported(DurationFieldType.hours())) { periodFormatterBuilder.appendHours().appendSuffix( RenderUtils.getResourceString(enumerationBundle, CLASS_NAME + DurationFieldType.hours().getName() + SHORT)); if (period.isSupported(DurationFieldType.minutes())) { periodFormatterBuilder
String periodString = "98d 01h 23m 45s"; PeriodParser parser = new PeriodFormatterBuilder() .appendDays().appendSuffix("d ") .appendHours().appendSuffix("h ") .appendMinutes().appendSuffix("m ") .appendSeconds().appendSuffix("s ") .toParser(); MutablePeriod period = new MutablePeriod(); parser.parseInto(period, periodString, 0, Locale.getDefault()); long millis = period.toDurationFrom(new DateTime(0)).getMillis();
/** * Adds to each field of this period. * * @param years amount of years to add to this period, which must be zero if unsupported * @param months amount of months to add to this period, which must be zero if unsupported * @param weeks amount of weeks to add to this period, which must be zero if unsupported * @param days amount of days to add to this period, which must be zero if unsupported * @param hours amount of hours to add to this period, which must be zero if unsupported * @param minutes amount of minutes to add to this period, which must be zero if unsupported * @param seconds amount of seconds to add to this period, which must be zero if unsupported * @param millis amount of milliseconds to add to this period, which must be zero if unsupported * @throws IllegalArgumentException if the period being added contains a field * not supported by this period * @throws ArithmeticException if the addition exceeds the capacity of the period */ public void add(int years, int months, int weeks, int days, int hours, int minutes, int seconds, int millis) { setPeriod( FieldUtils.safeAdd(getYears(), years), FieldUtils.safeAdd(getMonths(), months), FieldUtils.safeAdd(getWeeks(), weeks), FieldUtils.safeAdd(getDays(), days), FieldUtils.safeAdd(getHours(), hours), FieldUtils.safeAdd(getMinutes(), minutes), FieldUtils.safeAdd(getSeconds(), seconds), FieldUtils.safeAdd(getMillis(), millis) ); }
/** * Helper method to negate a copy of period * * @param mPeriod * @return a negated copy of period */ static Period negatePeriod(Period period) { MutablePeriod mPeriod = period.toMutablePeriod(); mPeriod.setYears(-mPeriod.getYears()); mPeriod.setMonths(-mPeriod.getMonths()); mPeriod.setWeeks(-mPeriod.getWeeks()); mPeriod.setDays(-mPeriod.getDays()); mPeriod.setHours(-mPeriod.getHours()); mPeriod.setMinutes(-mPeriod.getMinutes()); mPeriod.setSeconds(-mPeriod.getSeconds()); mPeriod.setMillis(-mPeriod.getMillis()); return mPeriod.toPeriod(); }
return this; } 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); if (remainder != 0) { DurationFieldType f; throw new UnsupportedOperationException("Unsupported duration type: " + field + " when dividing"); p.add(f, standardUnit * remainder); for (int i = 0; i < p.size(); i++) { p.setValue(i, p.getValue(i) / m);
/** * Get this object as a <code>MutablePeriod</code>. * <p> * This will always return a new <code>MutablePeriod</code> with the same fields. * * @return a MutablePeriod using the same field set and values */ public MutablePeriod toMutablePeriod() { return new MutablePeriod(this); }