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

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

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

Temporal.minus介绍

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

This method returns a new object based on this one with the specified period subtracted. For example, on a LocalDate, this could be used to subtract 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 March, then subtracting 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 behave in a manor equivalent to the default method behavior.

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上,这可以用来减去年、月或日数。返回的对象将具有与此对象相同的可观察类型。
在某些情况下,更改字段没有完全定义。例如,如果目标对象是代表3月31日的日期,那么减去一个月就不清楚了。在这种情况下,字段负责解决结果。通常,它会选择上一个有效日期,在本例中是2月的最后一个有效日期。
如果实现表示有边界的日期时间,例如LocalTime,那么允许的单位必须包括边界单位,但不能是边界单位的倍数。例如,LocalTime必须接受天,而不是周或月。
####实施者规范
实现必须在与默认方法行为等效的庄园中运行。
实现不能更改此对象或指定的时态对象。相反,必须返回经调整的原件副本。这为不可变和可变的实现提供了等效的、安全的行为。

代码示例

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

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

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

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

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

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

代码示例来源:origin: org.xbib/time

@Override
  public Temporal subtractFrom(Temporal temporal) {
    return temporal.minus(this);
  }
}

代码示例来源:origin: zeebe-io/zeebe

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

代码示例来源:origin: io.zeebe/zb-bpmn-model

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

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

/**
 * Subtracts this amount from the specified temporal object.
 * <p>
 * This returns a temporal object of the same observable type as the input
 * with this amount subtracted. This simply subtracts the period and duration from the temporal.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param temporal  the temporal object to adjust, not null
 * @return an object of the same type with the adjustment made, not null
 * @throws DateTimeException if unable to subtract
 * @throws UnsupportedTemporalTypeException if the DAYS unit is not supported
 * @throws ArithmeticException if numeric overflow occurs
 */
@Override
public Temporal subtractFrom(Temporal temporal) {
  return temporal.minus(period).minus(duration);
}

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

@Override
public Temporal subtractFrom( Temporal temporal )
{
  if ( months != 0 )
  {
    temporal = temporal.minus( months, MONTHS );
  }
  if ( days != 0 )
  {
    temporal = temporal.minus( days, DAYS );
  }
  if ( seconds != 0 )
  {
    temporal = temporal.minus( seconds, SECONDS );
  }
  if ( nanoseconds != 0 )
  {
    temporal = temporal.minus( nanoseconds, NANOS );
  }
  return temporal;
}

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

@Override
  public Temporal adjustInto(Temporal temporal) {
    int dow = temporal.get(DAY_OF_WEEK);
    switch (dow) {
      case 1:  // Monday
        return temporal.minus(3, DAYS);
      case 7:  // Sunday
        return temporal.minus(2, DAYS);
      default:
        return temporal.minus(1, DAYS);
    }
  }
},

代码示例来源: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.minus(1, DAYS);
      case 7:  // Sunday
        return temporal.minus(2, DAYS);
      default:
        return temporal;
    }
  }
}

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

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

代码示例来源:origin: com.datastax.oss/java-driver-core-shaded

/**
 * {@inheritDoc}
 *
 * <p>This implementation converts the months and days components to a {@link Period}, and the
 * nanosecond component to a {@link Duration}, and subtracts those two amounts to the temporal
 * object. Therefore the chronology of the temporal must be either the ISO chronology or null.
 *
 * @see Period#subtractFrom(Temporal)
 * @see Duration#subtractFrom(Temporal)
 */
@Override
public Temporal subtractFrom(Temporal temporal) {
 return temporal.minus(Period.of(0, months, days)).minus(Duration.ofNanos(nanoseconds));
}

代码示例来源:origin: org.ojalgo/ojalgo

public Temporal subtractFrom(final Temporal temporal) {
  return temporal.minus(this.toDurationInMillis(), CalendarDateUnit.MILLIS);
}

代码示例来源:origin: optimatika/ojAlgo

public Temporal subtractFrom(final Temporal temporal) {
  return temporal.minus(this.toDurationInMillis(), CalendarDateUnit.MILLIS);
}

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

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

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

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

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

@Override
  public Temporal adjustInto(Temporal temporal) {
    int calDow = temporal.get(DAY_OF_WEEK);
    if (relative < 2 && calDow == dowValue) {
      return temporal;
    }
    if ((relative & 1) == 0) {
      int daysDiff = calDow - dowValue;
      return temporal.plus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    } else {
      int daysDiff = dowValue - calDow;
      return temporal.minus(daysDiff >= 0 ? 7 - daysDiff : -daysDiff, DAYS);
    }
  }
}

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

@Override
public Temporal subtractFrom(Temporal temporal) {
  Jdk8Methods.requireNonNull(temporal, "temporal");
  Chronology temporalChrono = temporal.query(TemporalQueries.chronology());
  if (temporalChrono != null && chronology.equals(temporalChrono) == false) {
    throw new DateTimeException("Invalid chronology, required: " + chronology.getId() + ", but was: " + temporalChrono.getId());
  }
  if (years != 0) {
    temporal = temporal.minus(years, YEARS);
  }
  if (months != 0) {
    temporal = temporal.minus(months, MONTHS);
  }
  if (days != 0) {
    temporal = temporal.minus(days, DAYS);
  }
  return temporal;
}

代码示例来源:origin: hawkular/hawkular-metrics

public static TemporalAdjuster startOfPreviousEvenHour() {
  return temporal -> {
    int currentHour = temporal.get(ChronoField.HOUR_OF_DAY);
    return temporal.minus((currentHour % 2 == 0) ? 0 : 1, ChronoUnit.HOURS)
        .with(ChronoField.MINUTE_OF_HOUR, 0)
        .with(ChronoField.SECOND_OF_MINUTE, 0)
        .with(ChronoField.NANO_OF_SECOND, 0);
  };
}

代码示例来源:origin: Silverpeas/Silverpeas-Core

private Temporal computeDateForMonthlyFrequencyFrom(final Temporal source,
  DayOfWeekOccurrence dayOfWeek) {
 Temporal current = source;
 if (dayOfWeek.nth() > 1) {
  current = current.with(ChronoField.ALIGNED_WEEK_OF_MONTH, dayOfWeek.nth());
 } else if (dayOfWeek.nth() < 0) {
  current = current.with(ChronoField.DAY_OF_MONTH, 1)
    .plus(1, ChronoUnit.MONTHS)
    .minus(1, ChronoUnit.DAYS)
    .plus(dayOfWeek.nth(), ChronoUnit.WEEKS)
    .with(dayOfWeek.dayOfWeek());
 }
 return current;
}

相关文章