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

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

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

Period.withDays介绍

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

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

This instance is immutable and unaffected by this method call.
[中]返回指定天数的此期间的副本。
这将设置此期间副本中的天数单位。年和月单位不受影响。
此实例是不可变的,不受此方法调用的影响。

代码示例

代码示例来源: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.threeten/threeten-extra

/**
 * Returns a copy of this instance with the days and duration normalized using the standard day of 24 hours.
 * <p>
 * This normalizes the days and duration, leaving the years and months unchanged.
 * The result uses a standard day length of 24 hours.
 * <p>
 * This combines the duration seconds with the number of days and shares the total
 * seconds between the two fields. For example, a period of
 * "2 days and 86401 seconds" will be normalized to "3 days and 1 second".
 * <p>
 * The sign of the days and duration will be the same after normalization.
 * For example, a period of "1 day and -172801 seconds" will be normalized to
 * "-1 day and -1 second".
 * <p>
 * Note that no normalization is performed on the years or months.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @return a {@code PeriodDuration} based on this one with excess duration normalized to days, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
public PeriodDuration normalizedStandardDays() {
  long totalSecs = period.getDays() * SECONDS_PER_DAY + duration.getSeconds();
  int splitDays = Math.toIntExact(totalSecs / SECONDS_PER_DAY);
  long splitSecs = totalSecs % SECONDS_PER_DAY;
  if (splitDays == period.getDays() && splitSecs == duration.getSeconds()) {
    return this;
  }
  return PeriodDuration.of(period.withDays(splitDays), duration.withSeconds(splitSecs));
}

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

/**
 * Obtains a Period consisting of the number of years and months between two {@link java.time.YearMonth} instances.
 * The 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.YearMonth}, inclusive, not null
 * @param endExclusive   the end {@link java.time.YearMonth}, exclusive, not null
 * @return a Period between the year/months
 * @see java.time.Period#between(LocalDate, LocalDate)
 */
public static Period between(final Period type, YearMonth startInclusive, YearMonth endExclusive) {
  int dayOfMonth = 1;
  return Period.between(
      DateTimeExtensions.leftShift(startInclusive, dayOfMonth),
      DateTimeExtensions.leftShift(endExclusive, dayOfMonth))
      .withDays(0);
}

代码示例来源: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 = diffPeriod.withDays(0);
long monthDiff = diffPeriod.toTotalMonths() % period.toTotalMonths();
return start.plus(diffPeriod).minusMonths(monthDiff);
diffPeriod = diffPeriod.withDays(0);
diffPeriod.withMonths(0);
long yearsDiff = diffPeriod.getYears() % period.getYears();

代码示例来源:origin: org.kie/kie-dmn-feel

public FEELFnResult<TemporalAmount> invoke(@ParameterName("from") Temporal from, @ParameterName("to") Temporal to) {
  if ( from == null ) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "cannot be null"));
  }
  if ( to == null ) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "to", "cannot be null"));
  }
  final LocalDate fromDate = getLocalDateFromTemporal(from);
  if (fromDate == null) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "is of type not suitable for years and months function"));
  }
  final LocalDate toDate = getLocalDateFromTemporal(to);
  if (toDate == null) {
    return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "to", "is of type not suitable for years and months function"));
  }
  return FEELFnResult.ofResult( Period.between( fromDate, toDate ).withDays( 0 ) );
}

相关文章