@Override public int indexOf(final CalendarDay day) { return (int) Period .between(min.getDate().withDayOfMonth(1), day.getDate().withDayOfMonth(1)) .toTotalMonths(); }
/** * Obtains a {@code Period} representing a number of days. * <p> * The resulting period will have the specified days. * The years and months units will be zero. * * @param days the number of days, positive or negative * @return the period of days, not null */ public static Period ofDays(int days) { return create(0, 0, days); }
/** * Returns a copy of this period with the years and months normalized * using a 12 month year. * <p> * This normalizes the years and months units, leaving the days unit unchanged. * The months unit is adjusted to have an absolute value less than 11, * with the years unit being adjusted to compensate. For example, a period of * "1 Year and 15 months" will be normalized to "2 years and 3 months". * <p> * The sign of the years and months units will be the same after normalization. * For example, a period of "1 year and -25 months" will be normalized to * "-1 year and -1 month". * <p> * This normalization uses a 12 month year which is not valid for all calendar systems. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code Period} based on this period with excess months normalized to years, not null * @throws ArithmeticException if numeric overflow occurs */ public Period normalized() { long totalMonths = toTotalMonths(); long splitYears = totalMonths / 12; int splitMonths = (int) (totalMonths % 12); // no overflow if (splitYears == years && splitMonths == months) { return this; } return create(Jdk8Methods.safeToInt(splitYears), splitMonths, days); }
/** * Returns a copy of this period with the specified amount added. * <p> * This input amount is converted to a {@code Period} using {@code from(TemporalAmount)}. * This operates separately on the years, months and days. * <p> * For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days" * returns "3 years, 8 months and 5 days". * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the period to add, not null * @return a {@code Period} based on this period with the requested period added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plus(TemporalAmount amountToAdd) { Period amount = Period.from(amountToAdd); return create( Jdk8Methods.safeAdd(years, amount.years), Jdk8Methods.safeAdd(months, amount.months), Jdk8Methods.safeAdd(days, amount.days)); }
if (years != 0) { if (months != 0) { temporal = temporal.minus(toTotalMonths(), MONTHS); } else { temporal = temporal.minus(years, YEARS);
if (yearMatch != null || monthMatch != null || weekMatch != null || dayMatch != null) { try { int years = parseNumber(text, yearMatch, negate); int months = parseNumber(text, monthMatch, negate); int weeks = parseNumber(text, weekMatch, negate); int days = parseNumber(text, dayMatch, negate); days = Jdk8Methods.safeAdd(days, Jdk8Methods.safeMultiply(weeks, 7)); return create(years, months, days); } catch (NumberFormatException ex) { throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Period", text, 0).initCause(ex);
/** * Returns a new instance with each amount in this period negated. * * @return a {@code Period} based on this period with the amounts negated, not null * @throws ArithmeticException if numeric overflow occurs */ public Period negated() { return multipliedBy(-1); }
/** * Creates an instance. * * @param years the amount * @param months the amount * @param days the amount */ private static Period create(int years, int months, int days) { if ((years | months | days) == 0) { return ZERO; } return new Period(years, months, days); }
if (excessDays != null && excessDays.isZero() == false && date != null && time != null) { date = date.plus(excessDays); excessDays = Period.ZERO;
/** * Obtains a {@code Period} representing a number of months. * <p> * The resulting period will have the specified months. * The years and days units will be zero. * * @param months the number of months, positive or negative * @return the period of months, not null */ public static Period ofMonths(int months) { return create(0, months, 0); }
/** * Returns a copy of this period with the years and months normalized * using a 12 month year. * <p> * This normalizes the years and months units, leaving the days unit unchanged. * The months unit is adjusted to have an absolute value less than 11, * with the years unit being adjusted to compensate. For example, a period of * "1 Year and 15 months" will be normalized to "2 years and 3 months". * <p> * The sign of the years and months units will be the same after normalization. * For example, a period of "1 year and -25 months" will be normalized to * "-1 year and -1 month". * <p> * This normalization uses a 12 month year which is not valid for all calendar systems. * <p> * This instance is immutable and unaffected by this method call. * * @return a {@code Period} based on this period with excess months normalized to years, not null * @throws ArithmeticException if numeric overflow occurs */ public Period normalized() { long totalMonths = toTotalMonths(); long splitYears = totalMonths / 12; int splitMonths = (int) (totalMonths % 12); // no overflow if (splitYears == years && splitMonths == months) { return this; } return create(Jdk8Methods.safeToInt(splitYears), splitMonths, days); }
/** * Returns a copy of this period with the specified amount subtracted. * <p> * This input amount is converted to a {@code Period} using {@code from(TemporalAmount)}. * This operates separately on the years, months and days. * <p> * For example, "1 year, 6 months and 3 days" minus "2 years, 2 months and 2 days" * returns "-1 years, 4 months and 1 day". * <p> * This instance is immutable and unaffected by this method call. * * @param amountToSubtract the period to subtract, not null * @return a {@code Period} based on this period with the requested period subtracted, not null * @throws ArithmeticException if numeric overflow occurs */ public Period minus(TemporalAmount amountToSubtract) { Period amount = Period.from(amountToSubtract); return create( Jdk8Methods.safeSubtract(years, amount.years), Jdk8Methods.safeSubtract(months, amount.months), Jdk8Methods.safeSubtract(days, amount.days)); }
if (years != 0) { if (months != 0) { temporal = temporal.plus(toTotalMonths(), MONTHS); } else { temporal = temporal.plus(years, YEARS);
if (yearMatch != null || monthMatch != null || weekMatch != null || dayMatch != null) { try { int years = parseNumber(text, yearMatch, negate); int months = parseNumber(text, monthMatch, negate); int weeks = parseNumber(text, weekMatch, negate); int days = parseNumber(text, dayMatch, negate); days = Jdk8Methods.safeAdd(days, Jdk8Methods.safeMultiply(weeks, 7)); return create(years, months, days); } catch (NumberFormatException ex) { throw (DateTimeParseException) new DateTimeParseException("Text cannot be parsed to a Period", text, 0).initCause(ex);
/** * Returns a new instance with each amount in this period negated. * * @return a {@code Period} based on this period with the amounts negated, not null * @throws ArithmeticException if numeric overflow occurs */ public Period negated() { return multipliedBy(-1); }
/** * Creates an instance. * * @param years the amount * @param months the amount * @param days the amount */ private static Period create(int years, int months, int days) { if ((years | months | days) == 0) { return ZERO; } return new Period(years, months, days); }
if (excessDays != null && excessDays.isZero() == false && date != null && time != null) { date = date.plus(excessDays); excessDays = Period.ZERO;