java.time.Period.subtractFrom()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(118)

本文整理了Java中java.time.Period.subtractFrom()方法的一些代码示例,展示了Period.subtractFrom()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Period.subtractFrom()方法的具体详情如下:
包路径:java.time.Period
类名称:Period
方法名:subtractFrom

Period.subtractFrom介绍

[英]Subtracts this period from the specified temporal object.

This returns a temporal object of the same observable type as the input with this period subtracted.

In most cases, it is clearer to reverse the calling pattern by using Temporal#minus(TemporalAmount).

// these two lines are equivalent, but the second approach is recommended 
dateTime = thisPeriod.subtractFrom(dateTime); 
dateTime = dateTime.minus(thisPeriod);

The calculation operates as follows. First, the chronology of the temporal is checked to ensure it is ISO chronology or null. Second, if the months are zero, the years are added if non-zero, otherwise the combination of years and months is added if non-zero. Finally, any days are added. The calculation will subtract the years, then months, then days. Only non-zero amounts will be subtracted. If the date-time has a calendar system with a fixed number of months in a year, then the years and months will be combined before being subtracted.

This instance is immutable and unaffected by this method call.
[中]从指定的时间对象中减去该周期。
这将返回一个时间对象,该对象的可观察类型与减去该周期的输入相同。
在大多数情况下,使用Temporal#minus(TemporalAmount)反转调用模式更为清晰。

// these two lines are equivalent, but the second approach is recommended 
dateTime = thisPeriod.subtractFrom(dateTime); 
dateTime = dateTime.minus(thisPeriod);

计算过程如下。首先,检查时态的时间顺序,以确保它是ISO时间顺序或null。第二,如果月份为零,如果不为零,则将年份相加,否则如果不为零,则将年份和月份的组合相加。最后,添加任意天数。计算将减去年、月、日。只会减去非零金额。如果日期-时间有一个日历系统,在一年中有固定的月数,那么年和月将在减去之前合并。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: metatron-app/metatron-discovery

@Override
public Temporal subtractFrom(final Temporal temporal) {
 return duration.subtractFrom(period.subtractFrom(temporal));
}

代码示例来源:origin: OpenGamma/Strata

/**
 * Subtracts this tenor from the specified date.
 * <p>
 * This method implements {@link TemporalAmount}.
 * It is not intended to be called directly.
 * Use {@link LocalDate#minus(TemporalAmount)} instead.
 *
 * @param temporal  the temporal object to subtract from
 * @return the result with this tenor subtracted
 * @throws DateTimeException if unable to subtract
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal subtractFrom(Temporal temporal) {
 // special case for performance
 if (temporal instanceof LocalDate) {
  LocalDate date = (LocalDate) temporal;
  return plusDays(date.minusMonths(period.toTotalMonths()), -period.getDays());
 }
 return period.subtractFrom(temporal);
}

代码示例来源:origin: OpenGamma/Strata

/**
 * Subtracts the period of this frequency from the specified date.
 * <p>
 * This method implements {@link TemporalAmount}.
 * It is not intended to be called directly.
 * Use {@link LocalDate#minus(TemporalAmount)} instead.
 *
 * @param temporal  the temporal object to subtract from
 * @return the result with this frequency subtracted
 * @throws DateTimeException if unable to subtract
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal subtractFrom(Temporal temporal) {
 // special case for performance
 if (temporal instanceof LocalDate) {
  LocalDate date = (LocalDate) temporal;
  return date.minusMonths(period.toTotalMonths()).minusDays(period.getDays());
 }
 return period.subtractFrom(temporal);
}

相关文章