private long truncateMillisPeriod(final long t) { // toStandardDuration assumes days are always 24h, and hours are always 60 minutes, // which may not always be the case, e.g if there are daylight saving changes. if (chronology.days().isPrecise() && chronology.hours().isPrecise()) { final long millis = period.toStandardDuration().getMillis(); long offset = t % millis - origin % millis; if (offset < 0) { offset += millis; } return t - offset; } else { throw new UnsupportedOperationException( "Period cannot be converted to milliseconds as some fields mays vary in length with chronology " + chronology ); } }
/** * 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)); } }
/** * 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)); } }
private long truncateMillisPeriod(final long t) { // toStandardDuration assumes days are always 24h, and hours are always 60 minutes, // which may not always be the case, e.g if there are daylight saving changes. if (chronology.days().isPrecise() && chronology.hours().isPrecise()) { final long millis = period.toStandardDuration().getMillis(); long offset = t % millis - origin % millis; if (offset < 0) { offset += millis; } return t - offset; } else { throw new UnsupportedOperationException( "Period cannot be converted to milliseconds as some fields mays vary in length with chronology " + chronology.toString()); } }
/** * 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; }
/** * Returns a copy of this date plus the specified number of days. * <p> * This datetime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * DateMidnight added = dt.plusDays(6); * DateMidnight added = dt.plus(Period.days(6)); * DateMidnight added = dt.withFieldAdded(DurationFieldType.days(), 6); * </pre> * * @param days the amount of days to add, may be negative * @return the new datetime plus the increased days * @since 1.1 */ public DateMidnight plusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().add(getMillis(), days); return withMillis(instant); }
/** * 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; }
/** * Returns a copy of this date plus the specified number of days. * <p> * This LocalDate instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDate added = dt.plusDays(6); * LocalDate added = dt.plus(Period.days(6)); * LocalDate added = dt.withFieldAdded(DurationFieldType.days(), 6); * </pre> * * @param days the amount of days to add, may be negative * @return the new LocalDate plus the increased days */ public LocalDate plusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().add(getLocalMillis(), days); return withLocalMillis(instant); }
/** * Returns a copy of this datetime minus the specified number of days. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime subtracted = dt.minusDays(6); * LocalDateTime subtracted = dt.minus(Period.days(6)); * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); * </pre> * * @param days the amount of days to subtract, may be negative * @return the new LocalDateTime minus the increased days */ public LocalDateTime minusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().subtract(getLocalMillis(), days); return withLocalMillis(instant); }
/** * Returns a copy of this date minus the specified number of days. * <p> * This LocalDate instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDate subtracted = dt.minusDays(6); * LocalDate subtracted = dt.minus(Period.days(6)); * LocalDate subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); * </pre> * * @param days the amount of days to subtract, may be negative * @return the new LocalDate minus the increased days */ public LocalDate minusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().subtract(getLocalMillis(), days); return withLocalMillis(instant); }
/** * Returns a copy of this datetime plus the specified number of days. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime added = dt.plusDays(6); * LocalDateTime added = dt.plus(Period.days(6)); * LocalDateTime added = dt.withFieldAdded(DurationFieldType.days(), 6); * </pre> * * @param days the amount of days to add, may be negative * @return the new LocalDateTime plus the increased days */ public LocalDateTime plusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().add(getLocalMillis(), days); return withLocalMillis(instant); }
/** * Returns a copy of this date minus the specified number of days. * <p> * This datetime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * DateMidnight subtracted = dt.minusDays(6); * DateMidnight subtracted = dt.minus(Period.days(6)); * DateMidnight subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); * </pre> * * @param days the amount of days to subtract, may be negative * @return the new datetime minus the increased days * @since 1.1 */ public DateMidnight minusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().subtract(getMillis(), days); return withMillis(instant); }
/** * 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; }
/** * 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; }
/** * Returns a copy of this date minus the specified number of days. * <p> * This LocalDate instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDate subtracted = dt.minusDays(6); * LocalDate subtracted = dt.minus(Period.days(6)); * LocalDate subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); * </pre> * * @param days the amount of days to subtract, may be negative * @return the new LocalDate minus the increased days */ public LocalDate minusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().subtract(getLocalMillis(), days); return withLocalMillis(instant); }
/** * Returns a copy of this datetime minus the specified number of days. * <p> * This LocalDateTime instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDateTime subtracted = dt.minusDays(6); * LocalDateTime subtracted = dt.minus(Period.days(6)); * LocalDateTime subtracted = dt.withFieldAdded(DurationFieldType.days(), -6); * </pre> * * @param days the amount of days to subtract, may be negative * @return the new LocalDateTime minus the increased days */ public LocalDateTime minusDays(int days) { if (days == 0) { return this; } long instant = getChronology().days().subtract(getLocalMillis(), days); return withLocalMillis(instant); }
/** * 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; }
/** * 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; }
/** * Creates a <code>Days</code> representing the number of whole days * between the two specified partial datetimes. * <p> * The two partials must contain the same fields, for example you can specify * two <code>LocalDate</code> objects. * * @param start the start partial date, must not be null * @param end the end partial date, must not be null * @return the period in days * @throws IllegalArgumentException if the partials are null or invalid */ public static Days daysBetween(ReadablePartial start, ReadablePartial end) { if (start instanceof LocalDate && end instanceof LocalDate) { Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); int days = chrono.days().getDifference( ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis()); return Days.days(days); } int amount = BaseSingleFieldPeriod.between(start, end, ZERO); return Days.days(amount); }
/** * Creates a <code>Days</code> representing the number of whole days * between the two specified partial datetimes. * <p> * The two partials must contain the same fields, for example you can specify * two <code>LocalDate</code> objects. * * @param start the start partial date, must not be null * @param end the end partial date, must not be null * @return the period in days * @throws IllegalArgumentException if the partials are null or invalid */ public static Days daysBetween(ReadablePartial start, ReadablePartial end) { if (start instanceof LocalDate && end instanceof LocalDate) { Chronology chrono = DateTimeUtils.getChronology(start.getChronology()); int days = chrono.days().getDifference( ((LocalDate) end).getLocalMillis(), ((LocalDate) start).getLocalMillis()); return Days.days(days); } int amount = BaseSingleFieldPeriod.between(start, end, ZERO); return Days.days(amount); }