/** * 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)); }
/** * Returns a copy of this datetime with the hour 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 * hour of day changed. * * @param hour the hour 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 withHourOfDay(int hour) { return withMillis(getChronology().hourOfDay().set(getMillis(), hour)); }
/** * 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)); } }
/** * Convert a datetime from one chronology to another. */ private static long convertByYear(long instant, Chronology from, Chronology to) { return to.getDateTimeMillis (from.year().get(instant), from.monthOfYear().get(instant), from.dayOfMonth().get(instant), from.millisOfDay().get(instant)); }
private long setDayOfMonth(Chronology chrono, long instant) { if (iDayOfMonth >= 0) { instant = chrono.dayOfMonth().set(instant, iDayOfMonth); } else { instant = chrono.dayOfMonth().set(instant, 1); instant = chrono.monthOfYear().add(instant, 1); instant = chrono.dayOfMonth().add(instant, iDayOfMonth); } return instant; }
/** * Constructs an instance set to the local time defined by the specified * instant evaluated using the specified chronology. * <p> * If the chronology is null, ISO chronology in the default zone is used. * Once the constructor is completed, the zone is no longer used. * * @param instant the milliseconds from 1970-01-01T00:00:00Z * @param chronology the chronology, null means ISOChronology in default zone */ public LocalDate(long instant, Chronology chronology) { chronology = DateTimeUtils.getChronology(chronology); long localMillis = chronology.getZone().getMillisKeepLocal(DateTimeZone.UTC, instant); chronology = chronology.withUTC(); iLocalMillis = chronology.dayOfMonth().roundFloor(localMillis); iChronology = chronology; }
/** * Returns a copy of this datetime with the minute of hour updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * minute of hour changed. * * @param minute the minute of hour to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withMinuteOfHour(int minute) { return withMillis(getChronology().minuteOfHour().set(getMillis(), minute)); }
/** * Returns a copy of this datetime plus the specified number of months. * <p> * The calculation will do its best to only change the month field * retaining the same day of month. * However, in certain circumstances, it may be necessary to alter * smaller fields. For example, 2007-03-31 plus one month cannot result * in 2007-04-31, so the day of month is adjusted to 2007-04-30. * <p> * The following three lines are identical in effect: * <pre> * DateTime added = dt.plusMonths(6); * DateTime added = dt.plus(Period.months(6)); * DateTime added = dt.withFieldAdded(DurationFieldType.months(), 6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param months the amount of months to add, may be negative * @return the new datetime plus the increased months * @since 1.1 */ public DateTime plusMonths(int months) { if (months == 0) { return this; } long instant = getChronology().months().add(getMillis(), months); return withMillis(instant); }
/** * Returns a copy of this datetime minus the specified number of months. * <p> * The calculation will do its best to only change the month field * retaining the same day of month. * However, in certain circumstances, it may be necessary to alter * smaller fields. For example, 2007-05-31 minus one month cannot result * in 2007-04-31, so the day of month is adjusted to 2007-04-30. * <p> * The following three lines are identical in effect: * <pre> * DateTime subtracted = dt.minusMonths(6); * DateTime subtracted = dt.minus(Period.months(6)); * DateTime subtracted = dt.withFieldAdded(DurationFieldType.months(), -6); * </pre> * <p> * This datetime instance is immutable and unaffected by this method call. * * @param months the amount of months to subtract, may be negative * @return the new datetime minus the increased months * @since 1.1 */ public DateTime minusMonths(int months) { if (months == 0) { return this; } long instant = getChronology().months().subtract(getMillis(), months); 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 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)); }
/** * Returns a copy of this datetime with the second of minute field updated. * <p> * DateTime is immutable, so there are no set methods. * Instead, this method returns a new instance with the value of * second of minute changed. * * @param second the second of minute to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withSecondOfMinute(int second) { return withMillis(getChronology().secondOfMinute().set(getMillis(), second)); }
/** * Returns a copy of this datetime with the day of month 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 month changed. * * @param dayOfMonth the day of month to set * @return a copy of this object with the field set * @throws IllegalArgumentException if the value is invalid * @since 1.3 */ public DateTime withDayOfMonth(int dayOfMonth) { return withMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth)); }
/** * Returns a copy of this datetime minus 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 minus one year cannot result * in 2007-02-29, so the day of month is adjusted to 2007-02-28. * <p> * The following three lines are identical in effect: * <pre> * DateTime subtracted = dt.minusYears(6); * DateTime subtracted = dt.minus(Period.years(6)); * DateTime subtracted = 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 subtract, may be negative * @return the new datetime minus the increased years * @since 1.1 */ public DateTime minusYears(int years) { if (years == 0) { return this; } long instant = getChronology().years().subtract(getMillis(), years); return withMillis(instant); }
/** * A debugging string for the chronology. * * @return the debugging string */ public String toString() { return "LimitChronology[" + getBase().toString() + ", " + (getLowerLimit() == null ? "NoLimit" : getLowerLimit().toString()) + ", " + (getUpperLimit() == null ? "NoLimit" : getUpperLimit().toString()) + ']'; }
/** * Set the month of the year to the specified value. * * @param monthOfYear the month of the year * @throws IllegalArgumentException if the value is invalid */ public void setMonthOfYear(final int monthOfYear) { setMillis(getChronology().monthOfYear().set(getMillis(), monthOfYear)); }
/** * Set the day of the month to the specified value. * * @param dayOfMonth the day of the month * @throws IllegalArgumentException if the value is invalid */ public void setDayOfMonth(final int dayOfMonth) { setMillis(getChronology().dayOfMonth().set(getMillis(), dayOfMonth)); }
/** * Set the hour of the day to the specified value. * * @param hourOfDay the hour of day * @throws IllegalArgumentException if the value is invalid */ public void setHourOfDay(final int hourOfDay) { setMillis(getChronology().hourOfDay().set(getMillis(), hourOfDay)); }
/** * Set the minute of the hour to the specified value. * * @param minuteOfHour the minute of hour * @throws IllegalArgumentException if the value is invalid */ public void setMinuteOfHour(final int minuteOfHour) { setMillis(getChronology().minuteOfHour().set(getMillis(), minuteOfHour)); }
/** * Set the second of the minute to the specified value. * * @param secondOfMinute the second of minute * @throws IllegalArgumentException if the value is invalid */ public void setSecondOfMinute(final int secondOfMinute) { setMillis(getChronology().secondOfMinute().set(getMillis(), secondOfMinute)); }