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

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

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

Temporal.until介绍

[英]Calculates the period between this temporal and another temporal in terms of the specified unit.

This calculates the period between two temporals in terms of a single unit. The start and end points are this and the specified temporal. The result will be negative if the end is before the start. For example, the period in hours between two temporal objects can be calculated using startTime.until(endTime, HOURS).

The calculation returns a whole number, representing the number of complete units between the two temporals. For example, the period in hours between the times 11:30 and 13:29 will only be one hour as it is one minute short of two hours.

There are two equivalent ways of using this method. The first is to invoke this method directly. The second is to use TemporalUnit#between(Temporal,Temporal):

// these two lines are equivalent 
between = thisUnit.between(start, end); 
between = start.until(end, thisUnit);

The choice should be made based on which makes the code more readable.

For example, this method allows the number of days between two dates to be calculated:

long daysBetween = DAYS.between(start, end); 
// or alternatively 
long daysBetween = start.until(end, DAYS);

Specification for implementors

Implementations must begin by checking to ensure that the input temporal object is of the same observable type as the implementation. They must then perform the calculation for all instances of ChronoUnit. A DateTimeException must be thrown for ChronoUnitinstances that are unsupported.

If the unit is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.between(Temporal, Temporal)passing this as the first argument and the input temporal as the second argument.

In summary, implementations must behave in a manner equivalent to this code:

// check input temporal is the same type as this class 
if (unit instanceof ChronoUnit) { 
// if unit is supported, then calculate and return result 
// else throw DateTimeException for unsupported units 
} 
return unit.between(this, endTemporal);

The target object must not be altered by this method.
[中]根据指定的单位计算此时间段和另一个时间段之间的周期。
这将以单个单位计算两个时间间隔。开始点和结束点是这个时间点和指定的时间点。如果结束早于开始,结果将为负值。例如,可以使用startTime计算两个时间对象之间的时间段(以小时为单位)。直到(结束时间,小时)。
计算返回一个整数,表示两个时间单位之间的完整单位数。例如,时间11:30和13:29之间的时间段(以小时为单位)将仅为一小时,因为它比两小时短一分钟。
使用这种方法有两种等效方法。第一种方法是直接调用这个方法。第二种是使用TemporalUnit#between(Temporal,Temporal):

// these two lines are equivalent 
between = thisUnit.between(start, end); 
between = start.until(end, thisUnit);

选择应该基于哪个选项使代码更具可读性。
例如,此方法允许计算两个日期之间的天数:

long daysBetween = DAYS.between(start, end); 
// or alternatively 
long daysBetween = start.until(end, DAYS);

####实施者规范
实现必须从检查开始,以确保输入的时态对象与实现具有相同的可观察类型。然后,他们必须对ChronoUnit的所有实例执行计算。必须为不受支持的ChronounitInstance引发DateTimeException。
如果该单元不是ChronoUnit,则通过调用TemporalUnit获得该方法的结果。(时态,时态)将其作为第一个参数传递,将输入时态作为第二个参数传递。
总之,实现的行为方式必须与以下代码相同:

// check input temporal is the same type as this class 
if (unit instanceof ChronoUnit) { 
// if unit is supported, then calculate and return result 
// else throw DateTimeException for unsupported units 
} 
return unit.between(this, endTemporal);

此方法不能更改目标对象。

代码示例

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

@Override
public boolean hasNext() {
  return (count < 0 || num < count)
      && (to == null || next.until(to, byUnit) > 0 || (including && next.equals(to)));
}

代码示例来源: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: kiegroup/optaplanner

+ " cannot have a from (" + from + ") which is strictly higher than its to (" + to + ").");
long space = from.until(to, incrementUnitType);
Temporal expectedTo = from.plus(space, incrementUnitType);
if (!to.equals(expectedTo)) {

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

private static long assertValidUntil( Temporal from, Temporal to, TemporalUnit unit )
{
  try
  {
    return from.until( to, unit );
  }
  catch ( UnsupportedTemporalTypeException e )
  {
    throw new UnsupportedTemporalUnitException( e.getMessage(), e );
  }
  catch ( DateTimeException e )
  {
    throw new InvalidValuesArgumentException( e.getMessage(), e );
  }
}

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

try
  until = from.temporal().until( to, unit );

代码示例来源:origin: com.sonymobile/lumbermill-core

@Override
public long between(Temporal temporal1Inclusive, Temporal temporal2Exclusive) {
  return temporal1Inclusive.until(temporal2Exclusive, this);
}

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

@Override
public long between(Temporal temporal1, Temporal temporal2) {
  return temporal1.until(temporal2, this);
}

代码示例来源:origin: tech.tablesaw/tablesaw-core

@Override
public boolean hasNext() {
  return (count < 0 || num < count)
      && (to == null || next.until(to, byUnit) > 0 || (including && next.equals(to)));
}

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

@Override
public long between(Temporal temporal1, Temporal temporal2) {
  switch(this) {
    case WEEK_BASED_YEARS:
      return Jdk8Methods.safeSubtract(temporal2.getLong(WEEK_BASED_YEAR), temporal1.getLong(WEEK_BASED_YEAR));
    case QUARTER_YEARS:
      return temporal1.until(temporal2, MONTHS) / 3;
    default:
      throw new IllegalStateException("Unreachable");
  }
}

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

long secs = startInclusive.until(endExclusive, SECONDS);
long nanos = 0;
if (startInclusive.isSupported(NANO_OF_SECOND) && endExclusive.isSupported(NANO_OF_SECOND)) {
      secs = startInclusive.until(adjustedEnd, SECONDS);;

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

private static long assertValidUntil( Temporal from, Temporal to, TemporalUnit unit )
{
  try
  {
    return from.until( to, unit );
  }
  catch ( UnsupportedTemporalTypeException e )
  {
    throw new UnsupportedTemporalUnitException( e.getMessage(), e );
  }
  catch ( DateTimeException e )
  {
    throw new InvalidValuesArgumentException( e.getMessage(), e );
  }
}

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

try
  until = from.temporal().until( to, unit );

相关文章