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

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

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

Period.addTo介绍

[英]Adds this period to the specified temporal object.

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

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

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

The calculation will add the years, then months, then days. Only non-zero amounts will be added. 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 added.

This instance is immutable and unaffected by this method call.
[中]将此句点添加到指定的时间对象。
这将返回一个与添加了该句点的输入具有相同可观察类型的时间对象。
在大多数情况下,使用Temporal#plus(TemporalAmount)反转调用模式更为清晰。

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

计算将加上年、月、日。只会添加非零金额。如果日期时间有一个日历系统,一年中有固定的月数,那么在添加之前,年和月将合并。
此实例是不可变的,不受此方法调用的影响。

代码示例

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

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

代码示例来源:origin: stackoverflow.com

Duration oneDayDuration = Duration.ofDays(1);
Period oneDayPeriod = Period.ofDays(1);
ZonedDateTime beforeChange = Instant.parse("2015-10-25T00:00:00.00Z").atZone(ZoneId.of("Europe/Berlin"));
System.out.println(beforeChange);
System.out.println(oneDayDuration.addTo(beforeChange));
System.out.println(oneDayPeriod.addTo(beforeChange));

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

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

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

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

相关文章