/** * Returns a copy of this datetime with different millis. * <p> * The returned object will be either be a new instance or <code>this</code>. * Only the millis will change, the chronology and time zone are kept. * * @param newMillis the new millis, from 1970-01-01T00:00:00Z * @return a copy of this datetime with different millis */ public DateTime withMillis(long newMillis) { return (newMillis == getMillis() ? this : new DateTime(newMillis, getChronology())); }
/** * Returns a copy of this datetime with the year of era field updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * year of era changed. * * @param yearOfEra the year of era to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withYearOfEra(int yearOfEra) { return withMillis(getChronology().yearOfEra().set(getMillis(), yearOfEra)); }
/** * Returns a copy of this datetime with the specified duration added. * <p> * If the addition is zero, then <code>this</code> is returned. * * @param durationToAdd the duration to add to this one * @param scalar the amount of times to add, such as -1 to subtract once * @return a copy of this datetime with the duration added * @throws ArithmeticException if the new datetime exceeds the capacity of a long */ public DateTime withDurationAdded(long durationToAdd, int scalar) { if (durationToAdd == 0 || scalar == 0) { return this; } long instant = getChronology().add(getMillis(), durationToAdd, scalar); return withMillis(instant); }
/** * Get the era property which provides access to advanced functionality. * * @return the era property */ public Property era() { return new Property(this, getChronology().era()); }
/** * Returns a copy of this datetime with the year field updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * year changed. * * @param year the year to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withYear(int year) { return withMillis(getChronology().year().set(getMillis(), year)); }
/** * Converts this object to a <code>LocalDate</code> with the * same date and chronology. * * @return a LocalDate with the same date and chronology * @since 1.3 */ public LocalDate toLocalDate() { return new LocalDate(getMillis(), getChronology()); }
/** * Returns a copy of this datetime with the partial set of fields replacing those * from this instance. * <p> * For example, if the partial is a <code>TimeOfDay</code> then the time fields * would be changed in the returned instance. * If the partial is null, then <code>this</code> is returned. * * @param partial the partial set of fields to apply to this datetime, null ignored * @return a copy of this datetime with a different set of fields * @throws IllegalArgumentException if any value is invalid */ public DateTime withFields(ReadablePartial partial) { if (partial == null) { return this; } return withMillis(getChronology().set(partial, getMillis())); }
/** * Get the year property which provides access to advanced functionality. * * @return the year property */ public Property year() { return new Property(this, getChronology().year()); }
/** * Returns a copy of this datetime with the month of year field updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * month of year changed. * * @param monthOfYear the month of year to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withMonthOfYear(int monthOfYear) { return withMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear)); }
/** * Converts this object to a <code>LocalDate</code> with the * same date and chronology. * * @return a LocalDate with the same date and chronology * @since 1.3 */ public LocalDate toLocalDate() { return new LocalDate(getMillis(), getChronology()); }
/** * Returns a copy of this datetime with the specified duration added. * <p> * If the addition is zero, then <code>this</code> is returned. * * @param durationToAdd the duration to add to this one * @param scalar the amount of times to add, such as -1 to subtract once * @return a copy of this datetime with the duration added * @throws ArithmeticException if the new datetime exceeds the capacity of a long */ public DateTime withDurationAdded(long durationToAdd, int scalar) { if (durationToAdd == 0 || scalar == 0) { return this; } long instant = getChronology().add(getMillis(), durationToAdd, scalar); return withMillis(instant); }
/** * Get the month of year property which provides access to advanced functionality. * * @return the month of year property */ public Property monthOfYear() { return new Property(this, getChronology().monthOfYear()); }
/** * Returns a copy of this datetime with the day of week field updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * day of week changed. * * @param dayOfWeek the day of week to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withDayOfWeek(int dayOfWeek) { return withMillis(getChronology().dayOfWeek().set(getMillis(), dayOfWeek)); }
/** * Returns a copy of this datetime with different millis. * <p> * The returned object will be either be a new instance or <code>this</code>. * Only the millis will change, the chronology and time zone are kept. * * @param newMillis the new millis, from 1970-01-01T00:00:00Z * @return a copy of this datetime with different millis */ public DateTime withMillis(long newMillis) { return (newMillis == getMillis() ? this : new DateTime(newMillis, getChronology())); }
/** * Returns a copy of this datetime with the partial set of fields replacing those * from this instance. * <p> * For example, if the partial is a <code>TimeOfDay</code> then the time fields * would be changed in the returned instance. * If the partial is null, then <code>this</code> is returned. * * @param partial the partial set of fields to apply to this datetime, null ignored * @return a copy of this datetime with a different set of fields * @throws IllegalArgumentException if any value is invalid */ public DateTime withFields(ReadablePartial partial) { if (partial == null) { return this; } return withMillis(getChronology().set(partial, getMillis())); }
/** * Get the day of week property which provides access to advanced functionality. * * @return the day of week property */ public Property dayOfWeek() { return new Property(this, getChronology().dayOfWeek()); }
/** * Returns a copy of this datetime with the millis of day field updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * millis of day changed. * * @param millis the millis of day to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withMillisOfDay(int millis) { return withMillis(getChronology().millisOfDay().set(getMillis(), millis)); }
@Override public DateTime bucketStart(DateTime time) { long t = time.getMillis(); final long duration = getDurationMillis(); long offset = t % duration - origin; if (offset < 0) { offset += duration; } return new DateTime(t - offset, time.getChronology()); }
/** * Returns a copy of this datetime with the specified period added. * <p> * If the addition is zero, then <code>this</code> is returned. * <p> * This method is typically used to add multiple copies of complex * period instances. Adding one field is best achieved using methods * like {@link #withFieldAdded(DurationFieldType, int)} * or {@link #plusYears(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 datetime with the period added * @throws ArithmeticException if the new datetime exceeds the capacity of a long */ public DateTime withPeriodAdded(ReadablePeriod period, int scalar) { if (period == null || scalar == 0) { return this; } long instant = getChronology().add(period, getMillis(), scalar); return withMillis(instant); }
/** * Get the minute of day property which provides access to advanced functionality. * * @return the minute of day property */ public Property minuteOfDay() { return new Property(this, getChronology().minuteOfDay()); }