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

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

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

Period.withMonths介绍

[英]Returns a copy of this period with the specified amount of months.

This sets the amount of the months unit in a copy of this period. The years and days units are unaffected.

The months unit is not normalized with the years unit. This means that a period of "15 months" is different to a period of "1 year and 3 months".

This instance is immutable and unaffected by this method call.
[中]返回指定月份数的此期间的副本。
这将设置此期间副本中的月数单位。年和日单位不受影响。
月份单位与年份单位不一致。这意味着“15个月”不同于“1年3个月”。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * Supports the unary plus operator; returns a {@link java.time.Period} with all unit values positive.
 * For example, a period of "2 years, -3 months, and -4 days" would result in a period of
 * "2 years, 3 months, and 4 days." No normalization is performed.
 *
 * @param self a Period
 * @return a positive Period
 * @since 2.5.0
 */
public static Period positive(final Period self) {
  return !self.isNegative() ? self : self.withDays(Math.abs(self.getDays()))
      .withMonths(Math.abs(self.getMonths()))
      .withYears(Math.abs(self.getYears()));
}

代码示例来源:origin: org.codehaus.groovy/groovy-datetime

/**
 * Obtains a Period consisting of the number of years between two {@link java.time.Year} instances.
 * The months and days of the Period will be zero.
 * The result of this method can be a negative period if the end is before the start.
 *
 * @param type           placeholder variable used by Groovy categories; ignored for default static methods
 * @param startInclusive the start {@link java.time.Year}, inclusive, not null
 * @param endExclusive   the end {@link java.time.Year}, exclusive, not null
 * @return a Period between the years
 * @see java.time.Period#between(LocalDate, LocalDate)
 */
public static Period between(final Period type, Year startInclusive, Year endExclusive) {
  MonthDay now = MonthDay.of(Month.JANUARY, 1);
  return Period.between(
      DateTimeExtensions.leftShift(startInclusive, now),
      DateTimeExtensions.leftShift(endExclusive, now))
      .withDays(0)
      .withMonths(0);
}

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

diffPeriod.withMonths(0);
long yearsDiff = diffPeriod.getYears() % period.getYears();
return start.plus(diffPeriod).minusYears(yearsDiff);

相关文章