/** * 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; }
/** * Gets the values of a period from an interval. * * @param period the period instant to use * @param duration the duration to query * @return the values of the period extracted from the duration */ public int[] get(ReadablePeriod period, long duration) { int size = period.size(); int[] values = new int[size]; if (duration != 0) { long current = 0; for (int i = 0; i < size; i++) { DurationField field = period.getFieldType(i).getField(this); if (field.isPrecise()) { int value = field.getDifference(duration, current); current = field.add(current, value); values[i] = value; } } } return values; }
ZonedDurationField(DurationField field, DateTimeZone zone) { super(field.getType()); if (!field.isSupported()) { throw new IllegalArgumentException(); } iField = field; iTimeField = useTimeArithmetic(field); iZone = zone; }
static int compareReverse(DurationField a, DurationField b) { if (a == null || !a.isSupported()) { if (b == null || !b.isSupported()) { return 0; } return -1; } if (b == null || !b.isSupported()) { return 1; } return -a.compareTo(b); } }
/** * Returns a copy of this datetime plus the specified number of millis. * <p> * The calculation will add a duration equivalent to the number of milliseconds. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusMillis(6); * DateTime added = dt.plus(Period.millis(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.millis(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param millis the amount of millis to add, may be negative * @return the new datetime plus the increased millis * @since 1.1 */ public DateTime plusMillis(int millis) { if (millis == 0) { return this; } long instant = getChronology().millis().add(getMillis(), millis); return withMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of weeks. * <p> * The calculation operates as if it were adding the equivalent in days. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusWeeks(6); * DateTime added = dt.plus(Period.weeks(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.weeks(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param weeks the amount of weeks to add, may be negative * @return the new datetime plus the increased weeks * @since 1.1 */ public DateTime plusWeeks(int weeks) { if (weeks == 0) { return this; } long instant = getChronology().weeks().add(getMillis(), weeks); return withMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of minutes. * <p> * The calculation will add a duration equivalent to the number of minutes * expressed in milliseconds. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusMinutes(6); * DateTime added = dt.plus(Period.minutes(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.minutes(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param minutes the amount of minutes to add, may be negative * @return the new datetime plus the increased minutes * @since 1.1 */ public DateTime plusMinutes(int minutes) { if (minutes == 0) { return this; } long instant = getChronology().minutes().add(getMillis(), minutes); return withMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of seconds. * <p> * The calculation will add a duration equivalent to the number of seconds * expressed in milliseconds. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusSeconds(6); * DateTime added = dt.plus(Period.seconds(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.seconds(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param seconds the amount of seconds to add, may be negative * @return the new datetime plus the increased seconds * @since 1.1 */ public DateTime plusSeconds(int seconds) { if (seconds == 0) { return this; } long instant = getChronology().seconds().add(getMillis(), seconds); return withMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of years. * <p> * The calculation will do its best to only change the year field * retaining the same month of year. * However, in certain circumstances, it may be necessary to alter * smaller fields. For example, 2008-02-29 plus one year cannot result * in 2009-02-29, so the day of month is adjusted to 2009-02-28. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusYears(6); * DateTime added = dt.plus(Period.years(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.years(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param years the amount of years to add, may be negative * @return the new datetime plus the increased years * @since 1.1 */ public DateTime plusYears(int years) { if (years == 0) { return this; } long instant = getChronology().years().add(getMillis(), years); return withMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of hours. * <p> * The calculation will add a duration equivalent to the number of hours * expressed in milliseconds. * <p> * For example, if a spring daylight savings cutover is from 01:59 to 03:00 * then adding one hour to 01:30 will result in 03:30. This is a duration * of one hour later, even though the hour field value changed from 1 to 3. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusHours(6); * DateTime added = dt.plus(Period.hours(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.hours(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param hours the amount of hours to add, may be negative * @return the new datetime plus the increased hours * @since 1.1 */ public DateTime plusHours(int hours) { if (hours == 0) { return this; } long instant = getChronology().hours().add(getMillis(), hours); return withMillis(instant); }
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 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);
/** * Add a number of months to the date. * * @param months the months to add * @throws IllegalArgumentException if the value is invalid */ public void addMonths(final int months) { if (months != 0) { setMillis(getChronology().months().add(getMillis(), months)); } }
/** * Adds to the instant specifying the duration and multiple to add. * * @param type a field type, usually obtained from DateTimeFieldType, not null * @param amount the amount to add of this duration * @throws IllegalArgumentException if the value is null or invalid * @throws ArithmeticException if the result exceeds the capacity of the instant */ public void add(DurationFieldType type, int amount) { if (type == null) { throw new IllegalArgumentException("Field must not be null"); } if (amount != 0) { setMillis(type.getField(getChronology()).add(getMillis(), amount)); } }
/** * Add a number of years to the date. * * @param years the years to add * @throws IllegalArgumentException if the value is invalid */ public void addYears(final int years) { if (years != 0) { setMillis(getChronology().years().add(getMillis(), years)); } }
/** * Add a number of weeks to the date. * * @param weeks the weeks to add * @throws IllegalArgumentException if the value is invalid */ public void addWeeks(final int weeks) { if (weeks != 0) { setMillis(getChronology().weeks().add(getMillis(), weeks)); } }
/** * Add a number of days to the date. * * @param days the days to add * @throws IllegalArgumentException if the value is invalid */ public void addDays(final int days) { if (days != 0) { setMillis(getChronology().days().add(getMillis(), days)); } }
/** * Checks if the duration type specified is supported by this * local time 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 (TIME_DURATION_TYPES.contains(type) || field.getUnitMillis() < getChronology().days().getUnitMillis()) { return field.isSupported(); } return false; }
/** * Add a number of hours to the date. * * @param hours the hours to add * @throws IllegalArgumentException if the value is invalid */ public void addHours(final int hours) { if (hours != 0) { setMillis(getChronology().hours().add(getMillis(), hours)); } }
/** * Add a number of minutes to the date. * * @param minutes the minutes to add * @throws IllegalArgumentException if the value is invalid */ public void addMinutes(final int minutes) { if (minutes != 0) { setMillis(getChronology().minutes().add(getMillis(), minutes)); } }