工作中经常会遇到需要算出下一天,或者未来的某个时间。使用原来的Calendar和Date都不太方便,在这里强烈推荐LocalDate和LocalDateTime两个类,可以直接plus或minus,非常的方便。
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code LocalDate} with the specified number of years added.
* <p>
* This method adds the specified amount to the years field in three steps:
* <ol>
* <li>Add the input years to the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) plus one year would result in the
* invalid date 2009-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2009-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param yearsToAdd the years to add, may be negative
* @return a {@code LocalDate} based on this date with the years added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate plusYears(long yearsToAdd) {
if (yearsToAdd == 0) {
return this;
}
int newYear = YEAR.checkValidIntValue(year + yearsToAdd); // safe overflow
return resolvePreviousValid(newYear, month, day);
}
/**
* Returns a copy of this {@code LocalDate} with the specified number of months added.
* <p>
* This method adds the specified amount to the months field in three steps:
* <ol>
* <li>Add the input months to the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2007-03-31 plus one month would result in the invalid date
* 2007-04-31. Instead of returning an invalid result, the last valid day
* of the month, 2007-04-30, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthsToAdd the months to add, may be negative
* @return a {@code LocalDate} based on this date with the months added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate plusMonths(long monthsToAdd) {
if (monthsToAdd == 0) {
return this;
}
long monthCount = year * 12L + (month - 1);
long calcMonths = monthCount + monthsToAdd; // safe overflow
int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
int newMonth = (int)Math.floorMod(calcMonths, 12) + 1;
return resolvePreviousValid(newYear, newMonth, day);
}
/**
* Returns a copy of this {@code LocalDate} with the specified number of weeks added.
* <p>
* This method adds the specified amount in weeks to the days field incrementing
* the month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2008-12-31 plus one week would result in 2009-01-07.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param weeksToAdd the weeks to add, may be negative
* @return a {@code LocalDate} based on this date with the weeks added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate plusWeeks(long weeksToAdd) {
return plusDays(Math.multiplyExact(weeksToAdd, 7));
}
/**
* Returns a copy of this {@code LocalDate} with the specified number of days added.
* <p>
* This method adds the specified amount to the days field incrementing the
* month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2008-12-31 plus one day would result in 2009-01-01.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param daysToAdd the days to add, may be negative
* @return a {@code LocalDate} based on this date with the days added, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate plusDays(long daysToAdd) {
if (daysToAdd == 0) {
return this;
}
long mjDay = Math.addExact(toEpochDay(), daysToAdd);
return LocalDate.ofEpochDay(mjDay);
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this date with the specified amount subtracted.
* <p>
* This returns a {@code LocalDate}, based on this one, with the specified amount subtracted.
* The amount is typically {@link Period} but may be any other type implementing
* the {@link TemporalAmount} interface.
* <p>
* The calculation is delegated to the amount object by calling
* {@link TemporalAmount#subtractFrom(Temporal)}. The amount implementation is free
* to implement the subtraction in any way it wishes, however it typically
* calls back to {@link #minus(long, TemporalUnit)}. Consult the documentation
* of the amount implementation to determine if it can be successfully subtracted.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToSubtract the amount to subtract, not null
* @return a {@code LocalDate} based on this date with the subtraction made, not null
* @throws DateTimeException if the subtraction cannot be made
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public LocalDate minus(TemporalAmount amountToSubtract) {
if (amountToSubtract instanceof Period) {
Period periodToSubtract = (Period) amountToSubtract;
return minusMonths(periodToSubtract.toTotalMonths()).minusDays(periodToSubtract.getDays());
}
Objects.requireNonNull(amountToSubtract, "amountToSubtract");
return (LocalDate) amountToSubtract.subtractFrom(this);
}
/**
* Returns a copy of this date with the specified amount subtracted.
* <p>
* This returns a {@code LocalDate}, based on this one, with the amount
* in terms of the unit subtracted. If it is not possible to subtract the amount,
* because the unit is not supported or for some other reason, an exception is thrown.
* <p>
* This method is equivalent to {@link #plus(long, TemporalUnit)} with the amount negated.
* See that method for a full description of how addition, and thus subtraction, works.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param amountToSubtract the amount of the unit to subtract from the result, may be negative
* @param unit the unit of the amount to subtract, not null
* @return a {@code LocalDate} based on this date with the specified amount subtracted, not null
* @throws DateTimeException if the subtraction cannot be made
* @throws UnsupportedTemporalTypeException if the unit is not supported
* @throws ArithmeticException if numeric overflow occurs
*/
@Override
public LocalDate minus(long amountToSubtract, TemporalUnit unit) {
return (amountToSubtract == Long.MIN_VALUE ? plus(Long.MAX_VALUE, unit).plus(1, unit) : plus(-amountToSubtract, unit));
}
//-----------------------------------------------------------------------
/**
* Returns a copy of this {@code LocalDate} with the specified number of years subtracted.
* <p>
* This method subtracts the specified amount from the years field in three steps:
* <ol>
* <li>Subtract the input years from the year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2008-02-29 (leap year) minus one year would result in the
* invalid date 2007-02-29 (standard year). Instead of returning an invalid
* result, the last valid day of the month, 2007-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param yearsToSubtract the years to subtract, may be negative
* @return a {@code LocalDate} based on this date with the years subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate minusYears(long yearsToSubtract) {
return (yearsToSubtract == Long.MIN_VALUE ? plusYears(Long.MAX_VALUE).plusYears(1) : plusYears(-yearsToSubtract));
}
/**
* Returns a copy of this {@code LocalDate} with the specified number of months subtracted.
* <p>
* This method subtracts the specified amount from the months field in three steps:
* <ol>
* <li>Subtract the input months from the month-of-year field</li>
* <li>Check if the resulting date would be invalid</li>
* <li>Adjust the day-of-month to the last valid day if necessary</li>
* </ol>
* <p>
* For example, 2007-03-31 minus one month would result in the invalid date
* 2007-02-31. Instead of returning an invalid result, the last valid day
* of the month, 2007-02-28, is selected instead.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param monthsToSubtract the months to subtract, may be negative
* @return a {@code LocalDate} based on this date with the months subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate minusMonths(long monthsToSubtract) {
return (monthsToSubtract == Long.MIN_VALUE ? plusMonths(Long.MAX_VALUE).plusMonths(1) : plusMonths(-monthsToSubtract));
}
/**
* Returns a copy of this {@code LocalDate} with the specified number of weeks subtracted.
* <p>
* This method subtracts the specified amount in weeks from the days field decrementing
* the month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2009-01-07 minus one week would result in 2008-12-31.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param weeksToSubtract the weeks to subtract, may be negative
* @return a {@code LocalDate} based on this date with the weeks subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate minusWeeks(long weeksToSubtract) {
return (weeksToSubtract == Long.MIN_VALUE ? plusWeeks(Long.MAX_VALUE).plusWeeks(1) : plusWeeks(-weeksToSubtract));
}
/**
* Returns a copy of this {@code LocalDate} with the specified number of days subtracted.
* <p>
* This method subtracts the specified amount from the days field decrementing the
* month and year fields as necessary to ensure the result remains valid.
* The result is only invalid if the maximum/minimum year is exceeded.
* <p>
* For example, 2009-01-01 minus one day would result in 2008-12-31.
* <p>
* This instance is immutable and unaffected by this method call.
*
* @param daysToSubtract the days to subtract, may be negative
* @return a {@code LocalDate} based on this date with the days subtracted, not null
* @throws DateTimeException if the result exceeds the supported date range
*/
public LocalDate minusDays(long daysToSubtract) {
return (daysToSubtract == Long.MIN_VALUE ? plusDays(Long.MAX_VALUE).plusDays(1) : plusDays(-daysToSubtract));
}
网友评论