迁移 org.joda.time.Duration 到 java.time.Duration

问题描述 投票:0回答:1

我想把这个基于joda库的java迁移到java.time

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Instant;

private DateTime createdDate;
private Duration executionTime;
private Instant requestTime;


  public void startTimerProcess() {
    this.requestTime = Instant.now();
  }

  public void endTimerProcess() {
    this.executionTime = new Duration(requestTime, Instant.now());
  }

我试过这个:

import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;

private OffsetDateTime createdDate;
private Duration executionTime;
private Instant requestTime;


  public void startTimerProcess() {
    this.requestTime = Instant.now();
  }

  public void endTimerProcess() {
    this.executionTime = Duration.of(requestTime.toEpochMilli(), Instant.now());
  }

对于线

Duration.of
我得到错误:

Required type: TemporalUnit
Provided: Instant

你能指导我什么是实施他的迁移的正确方法吗?

java jodatime java-time
1个回答
1
投票

如果你想要之间两个瞬间的持续时间,那么你正在寻找

Duration#between(Instant, Instant)

public void endTimerProcess() {
  this.executionTime = Duration.between(requestTime, Instant.now());
}
© www.soinside.com 2019 - 2024. All rights reserved.