我想把这个基于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

你能告诉我实施他迁移的正确方式是什么吗?

推荐答案

如果你想要between两个瞬间的持续时间,那么你正在寻找Duration#between(Instant, Instant):

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

Java相关问答推荐

如何转换Tue Feb 27 2024 16:35:30 GMT +0800 String至ZonedDateTime类型""

SQlite for Android无法使用json_group_array/json_object

使用Java Streams API比较两个不同的Java集合对象和一个公共属性

如果一个子类没有构造函数,超类也没有构造函数,那么为什么我可以构造子类的实例呢?

使用java访问具体子类特定方法的最佳方法是什么?

RESTful框架类字段是安全的还是不安全的

获取字符串中带空格的数字和Java中的字符

当返回Mono<;Something>;时,不会调用Mono<;void>;.flatMap

将java.util.Date转换为OffsetDateTime

Jenv-相同的Java版本,但带有前缀

在JDK 1.8源代码中,为什么使用A-B 0来确定哪个更大,而不是A B?

每次FXMLLoader调用ApplationConext.getBean(类)时创建@Component的新实例

在学习Spring时,通过构造函数参数0表达了不满意的依赖关系

在打开搜索结果时,如何让Eclipse打开整个文件?

基于Java中mm/dd/yy格式的最近日期对数组列表进行排序

在Java中使用StorageReference将数据从Firebase存储添加到数组列表

睡眠在 Spring Boot 中

如何在 Spring Security 版本 6.x 和 Spring Boot 3.x 中允许 http 删除端点

JPA - getSingleResult 与 LIMIT 1

如何在 IntelliJ 中获取 Visual Studio Code 的 Dark Modern Java colored颜色 语法