java.time.Duration.between()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(775)

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

Duration.between介绍

[英]Obtains an instance of Duration representing the duration between two instants.

Obtains a Duration representing the duration between two instants. This calculates the duration between two temporal objects of the same type. The difference in seconds is calculated using Temporal#until(Temporal,TemporalUnit). The difference in nanoseconds is calculated using by querying the ChronoUnit#NANO_OF_SECOND field.

The result of this method can be a negative period if the end is before the start. To guarantee to obtain a positive duration call abs() on the result.
[中]获取表示两个实例之间的持续时间的持续时间实例。
获取表示两个瞬间之间的持续时间的持续时间。这将计算相同类型的两个临时对象之间的持续时间。使用时间#直到(时间,时间单位)计算秒数差。以纳秒为单位的差异通过查询ChronoUnit#NANO_OF_SECOND字段来计算。
如果结束时间早于开始时间,则此方法的结果可能是负周期。要保证获得正的持续时间,请对结果调用abs()。

代码示例

代码示例来源:origin: lets-blade/blade

public static long getCostMS(Instant start) {
  return Duration.between(start, Instant.now()).toMillis();
}

代码示例来源:origin: lets-blade/blade

public static long getCostMS(Instant start) {
  return Duration.between(start, Instant.now()).toMillis();
}

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

double getTickProgress()
{
  long timeSinceLastTick = Duration.between(startOfLastTick, Instant.now()).toMillis();
  float tickProgress = (timeSinceLastTick % 600) / 600f;
  return tickProgress * Math.PI;
}

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

@Override
public String getText()
{
  Duration timeLeft = Duration.between(Instant.now(), endTime);
  int seconds = (int) (timeLeft.toMillis() / 1000L);
  int minutes = (seconds % 3600) / 60;
  int secs = seconds % 60;
  return String.format("%d:%02d", minutes, secs);
}

代码示例来源:origin: MovingBlocks/Terasology

private void updatePing(EntityRef entity) {
  if (startMap.containsKey(entity) && endMap.containsKey(entity)) {
    long pingTime = Duration.between(startMap.get(entity), endMap.get(entity)).toMillis();
    pingMap.put(entity, pingTime);
  }
}

代码示例来源:origin: GoogleContainerTools/jib

/**
 * Captures the time since last lap or creation, and resets the start time.
 *
 * @return the duration of the last lap, or since creation
 */
Duration lap() {
 Instant now = clock.instant();
 Duration duration = Duration.between(lapStartTime, now);
 lapStartTime = now;
 return duration;
}

代码示例来源:origin: GoogleContainerTools/jib

/**
  * Gets the total elapsed time since creation.
  *
  * @return the total elapsed time
  */
 Duration getElapsedTime() {
  return Duration.between(startTime, clock.instant());
 }
}

代码示例来源:origin: thinkaurelius/titan

public Duration elapsed() {
  if (null == start) {
    return Duration.ZERO;
  }
  final Instant stopTime = (null==stop? times.getTime() : stop);
  return Duration.between(start, stopTime);
}

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

private static void exec(Connection cxn, Migration migration) throws SQLException {
  cxn.setAutoCommit(false);
  try {
    Instant start = Instant.now();
    migration.run(cxn);
    cxn.commit();
    LOGGER.info("Data migration took {} ms", Duration.between(start, Instant.now()).toMillis());
  } catch (SQLException e) {
    LOGGER.error("Data migration failed: {}", e.getMessage(), e);
    cxn.rollback();
    throw e;
  }
}

代码示例来源:origin: JanusGraph/janusgraph

public Duration elapsed() {
  if (null == start) {
    return Duration.ZERO;
  }
  final Instant stopTime = (null==stop? times.getTime() : stop);
  return Duration.between(start, stopTime);
}

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

@Override
public boolean render()
{
  Duration timeLeft = Duration.between(Instant.now(), endTime);
  return !timeLeft.isNegative();
}

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

/**
 * Calculates how much time is left before the trap is collapsing.
 *
 * @return Value between 0 and 1. 0 means the trap was laid moments ago.
 * 1 is a trap that's about to collapse.
 */
public double getTrapTimeRelative()
{
  Duration duration = Duration.between(placedOn, Instant.now());
  return duration.compareTo(TRAP_TIME) < 0 ? (double) duration.toMillis() / TRAP_TIME.toMillis() : 1;
}

代码示例来源:origin: MovingBlocks/Terasology

/**
 * Refresh all the metric value and the bindingMap who notes user's authorization.
 */
private void refreshMetricsPeriodic() {
  if (Duration.between(timeForRefreshMetric, Instant.now()).getSeconds() > 5) {
    metrics.refreshAllMetrics();
    timeForRefreshMetric = Instant.now();
    if (bindingMap != null) {
      refreshBindingMap();
    }
  }
}

代码示例来源:origin: MovingBlocks/Terasology

private void recordPlayTime() {
  float playTime = Duration.between(lastRecordTime, Instant.now()).toMillis() / 1000f / 60;
  gamePlayStatsComponent.playTimeMinute += playTime;
  localPlayer.getCharacterEntity().addOrSaveComponent(gamePlayStatsComponent);
  lastRecordTime = Instant.now();
}

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

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
  Instant creationTime = this.metaData.getCreationTime();
  Duration maxInactiveInterval = this.metaData.getMaxInactiveInterval();
  Duration lastAccessedDuration = Duration.between(creationTime, this.metaData.getLastAccessedTime());
  out.defaultWriteObject();
  out.writeObject(creationTime);
  out.writeObject(maxInactiveInterval);
  out.writeObject(lastAccessedDuration);
}

代码示例来源:origin: thinkaurelius/titan

private Duration timeSinceFirstMsg() {
  Duration sinceFirst =  Duration.ZERO;
  if (!toSend.isEmpty()) {
    Instant firstTimestamp = toSend.get(0).message.getMessage().getTimestamp();
    Instant nowTimestamp   = times.getTime();
    if (firstTimestamp.compareTo(nowTimestamp) < 0) {
      sinceFirst = Duration.between(firstTimestamp, nowTimestamp);
    }
  }
  return sinceFirst;
}

代码示例来源:origin: SonarSource/sonarqube

private static SnapshotDto findNearestSnapshotToTargetDate(List<SnapshotDto> snapshots, Instant targetDate) {
 // FIXME shouldn't this be the first analysis after targetDate?
 Duration bestDuration = null;
 SnapshotDto nearest = null;
 for (SnapshotDto snapshot : snapshots) {
  Instant createdAt = Instant.ofEpochMilli(snapshot.getCreatedAt());
  Duration duration = Duration.between(targetDate, createdAt).abs();
  if (bestDuration == null || duration.compareTo(bestDuration) <= 0) {
   bestDuration = duration;
   nearest = snapshot;
  }
 }
 return nearest;
}

代码示例来源:origin: JanusGraph/janusgraph

private Duration timeSinceFirstMsg() {
  Duration sinceFirst =  Duration.ZERO;
  if (!toSend.isEmpty()) {
    Instant firstTimestamp = toSend.get(0).message.getMessage().getTimestamp();
    Instant nowTimestamp   = times.getTime();
    if (firstTimestamp.compareTo(nowTimestamp) < 0) {
      sinceFirst = Duration.between(firstTimestamp, nowTimestamp);
    }
  }
  return sinceFirst;
}

代码示例来源:origin: stanfordnlp/CoreNLP

public static String elapsedTime(Date d1, Date d2){
 try{
  Duration period = Duration.between(d1.toInstant(), d2.toInstant());
  // Note: this will become easier with Java 9, using toDaysPart() etc.
  long days = period.toDays();
  period = period.minusDays(days);
  long hours = period.toHours();
  period = period.minusHours(hours);
  long minutes = period.toMinutes();
  period = period.minusMinutes(minutes);
  long seconds = period.getSeconds();
  return days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
 } catch(java.lang.IllegalArgumentException e) {
  log.warn(e);
 }
 return "";
}

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

@Override
public boolean render()
{
  final boolean rendered = super.render();
  if (rendered)
  {
    final Duration fromStart = Duration.between(getStartTime(), Instant.now());
    return !fromStart.minus(timer.getInitialDelay()).isNegative();
  }
  return false;
}

相关文章