閏年
scala> :paste // Entering paste mode (ctrl-D to finish) val date = new LocalDate val date_1 = date.minusYears(1).plusDays(2) val date_2 = date.plusDays(2).minusYears(1) // Exiting paste mode, now interpreting. date: org.joda.time.LocalDate = 2020-02-27 date_1: org.joda.time.LocalDate = 2019-03-01 date_2: org.joda.time.LocalDate = 2019-02-28
2020/02/27 の1年前 -> 2019/2/27 の2日後 -> 2019/3/1 2020/02/27 の2日後 -> 2020/2/29 の1年前 -> 2019/2/28
LocalDate#minusYears
/** * Returns a copy of this date minus the specified number of years. * <p> * This subtracts the specified number of years from the date. * If subtracting years makes the day-of-month invalid, it is adjusted to the last valid day in the month. * This LocalDate instance is immutable and unaffected by this method call. * <p> * The following three lines are identical in effect: * <pre> * LocalDate subtracted = dt.minusYears(6); * LocalDate subtracted = dt.minus(Period.years(6)); * LocalDate subtracted = dt.withFieldAdded(DurationFieldType.years(), -6); * </pre> * * @param years the amount of years to subtract, may be negative * @return the new LocalDate minus the increased years */ public LocalDate minusYears(int years) { if (years == 0) { return this; } long instant = getChronology().years().subtract(getLocalMillis(), years); return withLocalMillis(instant); }
うん
If subtracting years makes the day-of-month invalid, it is adjusted to the last valid day in the month.
minusMonthsもしかり
つまり
scala> :paste // Entering paste mode (ctrl-D to finish) import org.joda.time.LocalDate val date = new LocalDate(2019,12,31) val date_1 = date.minusMonths(12) val date_2 = date.minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1).minusMonths(1). minusMonths(1).minusMonths(1) // Exiting paste mode, now interpreting. import org.joda.time.LocalDate date: org.joda.time.LocalDate = 2019-12-31 date_1: org.joda.time.LocalDate = 2018-12-31 date_2: org.joda.time.LocalDate = 2018-12-28