/** * 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); }
/** * Obtains a {@code Period} representing a number of years. * <p> * The resulting period will have the specified years. * The months and days units will be zero. * * @param years the number of years, positive or negative * @return the period of years, not null */ public static Period ofYears(int years) { return create(years, 0, 0); }
/** * 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); }
/** * Obtains a {@code Period} representing a number of years. * <p> * The resulting period will have the specified years. * The months and days units will be zero. * * @param years the number of years, positive or negative * @return the period of years, not null */ public static Period ofYears(int years) { return create(years, 0, 0); }
/** * 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); }
/** * 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); }
/** * Obtains a {@code Period} representing a number of years, months and days. * <p> * This creates an instance based on years, months and days. * * @param years the amount of years, may be negative * @param months the amount of months, may be negative * @param days the amount of days, may be negative * @return the period of years, months and days, not null */ public static Period of(int years, int months, int days) { return create(years, months, days); }
/** * Obtains a {@code Period} representing a number of years, months and days. * <p> * This creates an instance based on years, months and days. * * @param years the amount of years, may be negative * @param months the amount of months, may be negative * @param days the amount of days, may be negative * @return the period of years, months and days, not null */ public static Period of(int years, int months, int days) { return create(years, months, days); }
/** * Returns a copy of this period with the specified amount of days. * <p> * This sets the amount of the days unit in a copy of this period. * The years and months units are unaffected. * <p> * This instance is immutable and unaffected by this method call. * * @param days the days to represent, may be negative * @return a {@code Period} based on this period with the requested days, not null */ public Period withDays(int days) { if (days == this.days) { return this; } return create(years, months, days); }
/** * Returns a copy of this period with the specified amount of days. * <p> * This sets the amount of the days unit in a copy of this period. * The years and months units are unaffected. * <p> * This instance is immutable and unaffected by this method call. * * @param days the days to represent, may be negative * @return a {@code Period} based on this period with the requested days, not null */ public Period withDays(int days) { if (days == this.days) { return this; } return create(years, months, days); }
/** * Obtains a {@code Period} representing a number of weeks. * <p> * The resulting period will have days equal to the weeks multiplied by seven. * The years and months units will be zero. * * @param weeks the number of weeks, positive or negative * @return the period of days, not null */ public static Period ofWeeks(int weeks) { return create(0, 0, Jdk8Methods.safeMultiply(weeks, 7)); }
/** * Obtains a {@code Period} representing a number of weeks. * <p> * The resulting period will have days equal to the weeks multiplied by seven. * The years and months units will be zero. * * @param weeks the number of weeks, positive or negative * @return the period of days, not null */ public static Period ofWeeks(int weeks) { return create(0, 0, Jdk8Methods.safeMultiply(weeks, 7)); }
/** * Returns a copy of this period with the specified years added. * <p> * This adds the amount to the years unit in a copy of this period. * The months and days units are unaffected. * For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6 months and 3 days". * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToAdd the years to add, positive or negative * @return a {@code Period} based on this period with the specified years added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plusYears(long yearsToAdd) { if (yearsToAdd == 0) { return this; } return create(Jdk8Methods.safeToInt(Jdk8Methods.safeAdd(years, yearsToAdd)), months, days); }
/** * Returns a copy of this period with the specified months added. * <p> * This adds the amount to the months unit in a copy of this period. * The years and days units are unaffected. * For example, "1 year, 6 months and 3 days" plus 2 months returns "1 year, 8 months and 3 days". * <p> * This instance is immutable and unaffected by this method call. * * @param monthsToAdd the months to add, positive or negative * @return a {@code Period} based on this period with the specified months added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plusMonths(long monthsToAdd) { if (monthsToAdd == 0) { return this; } return create(years, Jdk8Methods.safeToInt(Jdk8Methods.safeAdd(months, monthsToAdd)), days); }
/** * Returns a copy of this period with the specified years added. * <p> * This adds the amount to the years unit in a copy of this period. * The months and days units are unaffected. * For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6 months and 3 days". * <p> * This instance is immutable and unaffected by this method call. * * @param yearsToAdd the years to add, positive or negative * @return a {@code Period} based on this period with the specified years added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plusYears(long yearsToAdd) { if (yearsToAdd == 0) { return this; } return create(Jdk8Methods.safeToInt(Jdk8Methods.safeAdd(years, yearsToAdd)), months, days); }
/** * Returns a copy of this period with the specified days added. * <p> * This adds the amount to the days unit in a copy of this period. * The years and months units are unaffected. * For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6 months and 5 days". * <p> * This instance is immutable and unaffected by this method call. * * @param daysToAdd the days to add, positive or negative * @return a {@code Period} based on this period with the specified days added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plusDays(long daysToAdd) { if (daysToAdd == 0) { return this; } return create(years, months, Jdk8Methods.safeToInt(Jdk8Methods.safeAdd(days, daysToAdd))); }
/** * Returns a copy of this period with the specified days added. * <p> * This adds the amount to the days unit in a copy of this period. * The years and months units are unaffected. * For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6 months and 5 days". * <p> * This instance is immutable and unaffected by this method call. * * @param daysToAdd the days to add, positive or negative * @return a {@code Period} based on this period with the specified days added, not null * @throws ArithmeticException if numeric overflow occurs */ public Period plusDays(long daysToAdd) { if (daysToAdd == 0) { return this; } return create(years, months, Jdk8Methods.safeToInt(Jdk8Methods.safeAdd(days, daysToAdd))); }
/** * Returns a new instance with each element in this period multiplied * by the specified scalar. * <p> * This simply multiplies each field, years, months, days and normalized time, * by the scalar. No normalization is performed. * * @param scalar the scalar to multiply by, not null * @return a {@code Period} based on this period with the amounts multiplied by the scalar, not null * @throws ArithmeticException if numeric overflow occurs */ public Period multipliedBy(int scalar) { if (this == ZERO || scalar == 1) { return this; } return create( Jdk8Methods.safeMultiply(years, scalar), Jdk8Methods.safeMultiply(months, scalar), Jdk8Methods.safeMultiply(days, scalar)); }
/** * Returns a new instance with each element in this period multiplied * by the specified scalar. * <p> * This simply multiplies each field, years, months, days and normalized time, * by the scalar. No normalization is performed. * * @param scalar the scalar to multiply by, not null * @return a {@code Period} based on this period with the amounts multiplied by the scalar, not null * @throws ArithmeticException if numeric overflow occurs */ public Period multipliedBy(int scalar) { if (this == ZERO || scalar == 1) { return this; } return create( Jdk8Methods.safeMultiply(years, scalar), Jdk8Methods.safeMultiply(months, scalar), Jdk8Methods.safeMultiply(days, scalar)); }
/** * 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)); }