@Override public DurationFieldType getRangeDurationType() { return DurationFieldType.years(); }
/** * Gets the duration field type, which is <code>months</code>. * * @return the period type */ public DurationFieldType getFieldType() { return DurationFieldType.months(); }
/** * Gets the duration field type, which is <code>days</code>. * * @return the period type */ public DurationFieldType getFieldType() { return DurationFieldType.days(); }
if (DurationFieldType.centuries().equals(field)) { f = DurationFieldType.years(); standardUnit = 100; } else if (DurationFieldType.years().equals(field)) { f = DurationFieldType.months(); standardUnit = 12; } else if (DurationFieldType.halfdays().equals(field)) { f = DurationFieldType.hours(); standardUnit = 12; } else if (DurationFieldType.days().equals(field)) { f = DurationFieldType.hours(); standardUnit = 24; } else if (DurationFieldType.hours().equals(field)) { f = DurationFieldType.minutes(); standardUnit = 60; } else if (DurationFieldType.minutes().equals(field)) { f = DurationFieldType.seconds(); standardUnit = 60; } else if (DurationFieldType.seconds().equals(field)) { f = DurationFieldType.millis(); standardUnit = 1000; } else if (DurationFieldType.months().equals(field)) { f = DurationFieldType.days(); standardUnit = 30; } else if (DurationFieldType.weeks().equals(field)) { f = DurationFieldType.days(); standardUnit = 7; } else if (DurationFieldType.millis().equals(field)) {
/** * Checks if the field type specified is supported by this * local date and chronology. * This can be used to avoid exceptions in {@link #get(DateTimeFieldType)}. * * @param type a field type, usually obtained from DateTimeFieldType * @return true if the field type is supported */ public boolean isSupported(DateTimeFieldType type) { if (type == null) { return false; } DurationFieldType durType = type.getDurationType(); if (DATE_DURATION_TYPES.contains(durType) || durType.getField(getChronology()).getUnitMillis() >= getChronology().days().getUnitMillis()) { return type.getField(getChronology()).isSupported(); } return false; }
/** * 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; }
return this; long instant = getLocalMillis(); Chronology chrono = getChronology(); for (int i = 0; i < period.size(); i++) { long value = FieldUtils.safeMultiply(period.getValue(i), scalar); DurationFieldType type = period.getFieldType(i); if (isSupported(type)) { instant = type.getField(chrono).add(instant, value);
if (count > 0) { DurationField months = DurationFieldType.months().getField(iChrono); DurationField days = DurationFieldType.days().getField(iChrono); DurationField first = savedFields[0].iField.getDurationField(); if (compareReverse(first, months) >= 0 && compareReverse(first, days) <= 0) { saveField(DateTimeFieldType.year(), iDefaultYear); return computeMillis(resetFields, text); if (!savedFields[i].iField.isLenient()) { millis = savedFields[i].set(millis, i == (count - 1));
chronology = DateTimeUtils.getChronology(chronology).withUTC(); iChronology = chronology; if (types == null) { for (int i = 0; i < types.length; i++) { DateTimeFieldType loopType = types[i]; DurationField loopUnitField = loopType.getDurationType().getField(iChronology); if (i > 0) { if (loopUnitField.isSupported() == false) { if (lastUnitField.isSupported()) { throw new IllegalArgumentException("Types array must be in order largest-smallest: " + types[i - 1].getName() + " < " + loopType.getName()); } else { throw new IllegalArgumentException("Types array must not contain duplicate unsupported: " + int compare = lastUnitField.compareTo(loopUnitField); if (compare < 0) { throw new IllegalArgumentException("Types array must be in order largest-smallest: " + types[i - 1].getName() + " < " + loopType.getName()); DurationField lastRangeField = lastRangeType.getField(iChronology); DurationField loopRangeField = loopRangeType.getField(iChronology); if (lastRangeField.compareTo(loopRangeField) < 0) { throw new IllegalArgumentException("Types array must be in order largest-smallest: " + chronology.validate(this, values); iValues = (int[]) values.clone();
/** * Checks if the duration type specified is supported by this * local date and chronology. * * @param type a duration type, usually obtained from DurationFieldType * @return true if the field type is supported */ public boolean isSupported(DurationFieldType type) { if (type == null) { return false; } DurationField field = type.getField(getChronology()); if (DATE_DURATION_TYPES.contains(type) || field.getUnitMillis() >= getChronology().days().getUnitMillis()) { return field.isSupported(); } return false; }
DurationField unitField = fieldType.getDurationType().getField(iChronology); if (unitField.isSupported()) { for (; i < iTypes.length; i++) { DateTimeFieldType loopType = iTypes[i]; DurationField loopUnitField = loopType.getDurationType().getField(iChronology); if (loopUnitField.isSupported()) { int compare = unitField.compareTo(loopUnitField); if (compare > 0) { break; } else if (compare == 0) { if (fieldType.getRangeDurationType() == null) { break; continue; DurationField rangeField = fieldType.getRangeDurationType().getField(iChronology); DurationField loopRangeField = loopType.getRangeDurationType().getField(iChronology); if (rangeField.compareTo(loopRangeField) > 0) { break; iChronology.validate(newPartial, newValues); return newPartial; newValues = getField(index).set(this, index, newValues, value); return new Partial(this, newValues);
/** * 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; }
throw new IllegalArgumentException("Field must not be null"); if (isSupported(fieldType) == false) { throw new IllegalArgumentException("Field '" + fieldType + "' is not supported"); return this; long instant = fieldType.getField(getChronology()).add(getLocalMillis(), amount); return withLocalMillis(instant);
public static boolean isMoreSpecific(DateTimeFieldType df1, DateTimeFieldType df2, Chronology chronology) { DurationFieldType df1DurationFieldType = df1.getDurationType(); DurationFieldType df2DurationFieldType = df2.getDurationType(); if (!df2DurationFieldType.equals(df1DurationFieldType)) { DurationField df2Unit = df2DurationFieldType.getField(chronology); DurationFieldType p = df1.getRangeDurationType(); if (p != null) { DurationField df1Unit = df1DurationFieldType.getField(chronology); int cmp = df1Unit.compareTo(df2Unit); if (cmp < 0) { return true; } } } return false; }
/** * Get the number of milliseconds associated with a partition type. Eg * getUnitMilliseconds() of DatePartitionType.MINUTE = 60,000. */ public long getUnitMilliseconds() { return dateTimeField.getDurationType().getField(ISOChronology.getInstance()).getUnitMillis(); }
private static long millisBetween(ReadableInstant start, ReadableInstant end) { requireNonNull(start, "start is null"); requireNonNull(end, "end is null"); return millis().getField(getInstantChronology(start)).getDifferenceAsLong(end.getMillis(), start.getMillis()); }
public static Partial discardMoreSpecificFields(Partial p, DurationFieldType dft) { DurationField df = dft.getField(p.getChronology()); Partial res = new Partial(); for (int i = 0; i < p.size(); i++) { DateTimeFieldType fieldType = p.getFieldType(i); DurationField f = fieldType.getDurationType().getField(p.getChronology()); int cmp = df.compareTo(f); if (cmp <= 0) { res = res.with(fieldType, p.getValue(i)); } } return res; }
protected static Period getJodaTimePeriod(Partial p) { if (p.size() > 0) { DateTimeFieldType dtType = p.getFieldType(p.size()-1); DurationFieldType dType = dtType.getDurationType(); Period period = new Period(); if (period.isSupported(dType)) { return period.withField(dType, 1); } else { DurationField df = dType.getField(p.getChronology()); if (df instanceof ScaledDurationField) { ScaledDurationField sdf = (ScaledDurationField) df; return period.withField(sdf.getWrappedField().getType(), sdf.getScalar()); } // PeriodType.forFields(new DurationFieldType[]{dType}); // return new Period(df.getUnitMillis(), PeriodType.forFields(new DurationFieldType[]{dType})); } } return null; } public static Partial combineMoreGeneralFields(Partial p1, Partial p2) {
/** * Gets a type that defines just the years field. * * @return the period type */ public static PeriodType years() { PeriodType type = cYears; if (type == null) { type = new PeriodType( "Years", new DurationFieldType[] { DurationFieldType.years() }, new int[] { 0, -1, -1, -1, -1, -1, -1, -1, } ); cYears = type; } return type; }
/** * Returns a copy of this year-month minus the specified number of years. * <p> * This year-month instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * YearMonth subtracted = ym.minusYears(6); * YearMonth subtracted = ym.minus(Period.years(6)); * YearMonth subtracted = ym.withFieldAdded(DurationFieldType.years(), -6); * </pre> * * @param years the amount of years to subtract, may be negative * @return the new year-month minus the increased years, never null */ public YearMonth minusYears(int years) { return withFieldAdded(DurationFieldType.years(), FieldUtils.safeNegate(years)); }