java.time.temporal.Temporal.plus()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(272)

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

Temporal.plus介绍

[英]Returns an object of the same type as this object with the specified period added.

This method returns a new object based on this one with the specified period added. For example, on a LocalDate, this could be used to add a number of years, months or days. The returned object will have the same observable type as this object.

In some cases, changing a field is not fully defined. For example, if the target object is a date representing the 31st January, then adding one month would be unclear. In cases like this, the field is responsible for resolving the result. Typically it will choose the previous valid date, which would be the last valid day of February in this example.

If the implementation represents a date-time that has boundaries, such as LocalTime, then the permitted units must include the boundary unit, but no multiples of the boundary unit. For example, LocalTime must accept DAYS but not WEEKS or MONTHS.

Specification for implementors

Implementations must check and handle all units defined in ChronoUnit. If the unit is supported, then the addition must be performed. If unsupported, then a DateTimeException must be thrown.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.addTo(Temporal, long)passing this as the first argument.

Implementations must not alter either this object or the specified temporal object. Instead, an adjusted copy of the original must be returned. This provides equivalent, safe behavior for immutable and mutable implementations.
[中]返回与此对象类型相同且添加了指定句点的对象。
此方法返回基于此对象的新对象,并添加指定的句点。例如,在LocalDate上,这可以用于添加若干年、月或日。返回的对象将具有与此对象相同的可观察类型。
在某些情况下,更改字段没有完全定义。例如,如果目标对象是代表1月31日的日期,那么添加一个月就不清楚了。在这种情况下,字段负责解决结果。通常,它会选择上一个有效日期,在本例中是2月的最后一个有效日期。
如果实现表示有边界的日期时间,例如LocalTime,那么允许的单位必须包括边界单位,但不能是边界单位的倍数。例如,LocalTime必须接受天,而不是周或月。
####实施者规范
实现必须检查并处理IT中定义的所有单元。如果支持该装置,则必须进行添加。如果不支持,则必须抛出DateTimeException。
如果该单元不是ChronoUnit,则通过调用TemporalUnit获得该方法的结果。addTo(暂时的,长的)将此作为第一个参数传递。
实现不能更改此对象或指定的时态对象。相反,必须返回经调整的原件副本。这为不可变和可变的实现提供了等效的、安全的行为。

代码示例

代码示例来源:origin: jtablesaw/tablesaw

@Override
  @SuppressWarnings("unchecked")
  public T next() {
    final T current = next;
    next = (T) next.plus(by, byUnit);
    num++;
    return current;
  }
};

代码示例来源:origin: kiegroup/optaplanner

@Override
public Temporal_ get(long index) {
  if (index >= size || index < 0) {
    throw new IndexOutOfBoundsException();
  }
  return (Temporal_) from.plus(index * incrementUnitAmount, incrementUnitType);
}

代码示例来源:origin: neo4j/neo4j

private static Temporal assertValidPlus( Temporal temporal, long amountToAdd, TemporalUnit unit )
{
  try
  {
    return temporal.plus(amountToAdd,  unit);
  }
  catch ( DateTimeException | ArithmeticException e )
  {
    throw new TemporalArithmeticException( e.getMessage(), e );
  }
}

代码示例来源:origin: neo4j/neo4j

@Override
@SuppressWarnings( "unchecked" )
public final V plus( long amountToAdd, TemporalUnit unit )
{
  return replacement( (T) temporal().plus( amountToAdd, unit ) );
}

代码示例来源:origin: neo4j/neo4j

@Override
@SuppressWarnings( "unchecked" )
public final V plus( TemporalAmount amount )
{
  return replacement( (T) temporal().plus( amount ) );
}

代码示例来源:origin: kiegroup/optaplanner

@Override
public boolean contains(Temporal_ value) {
  if (value == null || !value.isSupported(incrementUnitType)) {
    return false;
  }
  // We cannot use Temporal.until() to check bounds due to rounding errors
  if (value.compareTo(from) < 0 || value.compareTo(to) >= 0) {
    return false;
  }
  long fromSpace = from.until(value, incrementUnitType);
  if (value.equals(from.plus(fromSpace + 1, incrementUnitType))) {
    // Temporal.until() rounds down, but it needs to round up, to be consistent with Temporal.plus()
    fromSpace++;
  }
  // Only checking the modulus is not enough: 1-MAR + 1 month doesn't include 7-MAR but the modulus is 0 anyway
  return fromSpace % incrementUnitAmount == 0
      && value.equals(from.plus(fromSpace, incrementUnitType));
}

代码示例来源:origin: debezium/debezium

/**
 * A utility method that adjusts <a href="https://dev.mysql.com/doc/refman/5.7/en/two-digit-years.html">ambiguous</a> 2-digit
 * year values of DATETIME, DATE, and TIMESTAMP types using these MySQL-specific rules:
 * <ul>
 * <li>Year values in the range 00-69 are converted to 2000-2069.</li>
 * <li>Year values in the range 70-99 are converted to 1970-1999.</li>
 * </ul>
 *
 * @param temporal the temporal instance to adjust; may not be null
 * @return the possibly adjusted temporal instance; never null
 */
protected static Temporal adjustTemporal(Temporal temporal) {
  if (temporal.isSupported(ChronoField.YEAR)) {
    int year = temporal.get(ChronoField.YEAR);
    if (0 <= year && year <= 69) {
      temporal = temporal.plus(2000, ChronoUnit.YEARS);
    } else if (70 <= year && year <= 99) {
      temporal = temporal.plus(1900, ChronoUnit.YEARS);
    }
  }
  return temporal;
}

代码示例来源:origin: kiegroup/optaplanner

Temporal expectedTo = from.plus(space, incrementUnitType);
if (!to.equals(expectedTo)) {
  Temporal roundedExpectedTo;
  try {
    roundedExpectedTo = from.plus(space, incrementUnitType);
  } catch (DateTimeException e) {
    throw new IllegalArgumentException("The " + getClass().getSimpleName()

代码示例来源:origin: neo4j/neo4j

static DurationValue durationBetween( Temporal from, Temporal to )
{
  long months = 0;
  long days = 0;
  if ( from.isSupported( EPOCH_DAY ) && to.isSupported( EPOCH_DAY ) )
  {
    months = assertValidUntil( from, to, ChronoUnit.MONTHS );
    try
    {
      from = from.plus( months, ChronoUnit.MONTHS );
    }
    catch ( DateTimeException | ArithmeticException e )
    {
      throw new TemporalArithmeticException( e.getMessage(), e );
    }
    days = assertValidUntil( from, to, ChronoUnit.DAYS );
    try
    {
      from = from.plus( days, ChronoUnit.DAYS );
    }
    catch ( DateTimeException | ArithmeticException e )
    {
      throw new TemporalArithmeticException( e.getMessage(), e );
    }
  }
  long nanos = assertValidUntil( from, to, NANOS );
  return newDuration( months, days, nanos / NANOS_PER_SECOND, nanos % NANOS_PER_SECOND );
}

代码示例来源:origin: neo4j/neo4j

assertEquals( b, a.plus( diffAB ), diffAB.prettyPrint() );
assertEquals( a, b.plus( diffBA ), diffBA.prettyPrint() );
assertEquals( b, a.plus( diffABs ), diffABs.prettyPrint() );
assertEquals( a, b.plus( diffBAs ), diffBAs.prettyPrint() );

代码示例来源:origin: eclipse/smarthome

@Override
public Temporal adjustInto(@Nullable Temporal temporal) {
  if (timeDone == null) {
    timeDone = temporal;
  }
  if (iterator.hasNext()) {
    current = iterator.next();
  }
  Temporal nextTime = timeDone.plus(current);
  timeDone = nextTime;
  return nextTime;
}

代码示例来源:origin: eclipse/smarthome

@Override
public Temporal adjustInto(@Nullable Temporal temporal) {
  // Never match the actual time, so since our basic
  // unit is seconds, we add one second.
  Temporal ret = temporal.plus(1, ChronoUnit.SECONDS);
  // We loop through the fields until they all match. If
  // one of them does not match, its type is incremented
  // and all lower fields are reset to their minimum. And
  // we start over with this new time.
  int index = 0;
  int length = fields.size();
  while (index < length) {
    final Field field = fields.get(index);
    final Temporal out = field.isOk(ret);
    if (out == null) {
      index++;
    } else {
      ret = out;
      index = 0;
    }
  }
  // All fields match!
  return ret;
}

代码示例来源:origin: eclipse/smarthome

@Nullable
Temporal isOk(Temporal t) {
  if (checker.matches(t)) {
    return null;
  }
  Temporal out = t.plus(1, type.getBaseUnit());
  // Fall-through switch case. for example if type is year all cases below must also be handled.
  switch (type) {
    case YEAR:
      out = out.with(ChronoField.MONTH_OF_YEAR, 1);
    case MONTH_OF_YEAR:
      out = out.with(ChronoField.DAY_OF_MONTH, 1);
    case DAY_OF_WEEK:
    case DAY_OF_MONTH:
      out = out.with(ChronoField.HOUR_OF_DAY, 0);
    case HOUR_OF_DAY:
      out = out.with(ChronoField.MINUTE_OF_HOUR, 0);
    case MINUTE_OF_HOUR:
      out = out.with(ChronoField.SECOND_OF_MINUTE, 0);
    case SECOND_OF_MINUTE:
      return out;
    default:
      throw new IllegalArgumentException("Invalid field type " + type);
  }
}

代码示例来源:origin: org.threeten/threeten-extra

@Override
  public Temporal adjustInto(Temporal temporal) {
    int dow = temporal.get(DAY_OF_WEEK);
    switch (dow) {
      case 6:  // Saturday
        return temporal.plus(2, DAYS);
      case 5:  // Friday
        return temporal.plus(3, DAYS);
      default:
        return temporal.plus(1, DAYS);
    }
  }
},

代码示例来源:origin: org.neo4j/neo4j-values

@Override
@SuppressWarnings( "unchecked" )
public final V plus( long amountToAdd, TemporalUnit unit )
{
  return replacement( (T) temporal().plus( amountToAdd, unit ) );
}

代码示例来源:origin: edu.byu.hbll/time

/**
 * Returns the "plus working days" adjuster, which returns a new date set for the specified number
 * of working days (defined as all days that are not Saturdays, Sundays, or BYU University
 * Holidays) in the future.
 * 
 * @param workingDays the number of working days to be added to a Temporal object 
 * @return the temporal adjuster as described
 */
public static TemporalAdjuster plusWorkingDays(int workingDays) {
 return temporal ->
   temporal.plus(toCalendarDays(toLocalDate(temporal), workingDays), ChronoUnit.DAYS);
}

代码示例来源:origin: org.neo4j/neo4j-values

@Override
@SuppressWarnings( "unchecked" )
public final V plus( TemporalAmount amount )
{
  return replacement( (T) temporal().plus( amount ) );
}

代码示例来源:origin: dremio/dremio-oss

@Override
 public Temporal adjustInto(Temporal temporal) {
  Temporal adjusted = temporal.with(adjuster);
  return (ChronoUnit.NANOS.between(temporal, adjusted) >= 0)
    ? adjusted
    : temporal.plus(amount).with(adjuster);
  }
};

代码示例来源:origin: com.github.seratch/java-time-backport

@SuppressWarnings("unchecked")
@Override
public <R extends Temporal> R adjustInto(R temporal, long newValue) {
  range().checkValidValue(newValue, this);
  return (R) temporal.plus(Jdk8Methods.safeSubtract(newValue, getFrom(temporal)), WEEKS);
}
@Override

代码示例来源:origin: com.github.seratch/java-time-backport

@Override
  public Temporal adjustInto(Temporal temporal) {
    switch (ordinal) {
      case 0: return temporal.with(DAY_OF_MONTH, 1);
      case 1: return temporal.with(DAY_OF_MONTH, temporal.range(DAY_OF_MONTH).getMaximum());
      case 2: return temporal.with(DAY_OF_MONTH, 1).plus(1, MONTHS);
      case 3: return temporal.with(DAY_OF_YEAR, 1);
      case 4: return temporal.with(DAY_OF_YEAR, temporal.range(DAY_OF_YEAR).getMaximum());
      case 5: return temporal.with(DAY_OF_YEAR, 1).plus(1, YEARS);
    }
    throw new IllegalStateException("Unreachable");
  }
}

相关文章