Migrate org.joda.time.Duration to java.time.Duration

84 Views Asked by At

I want to migrate this java based on joda library to 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());
  }

I tried this:

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());
  }

For the line Duration.of I get error:

Required type: TemporalUnit
Provided: Instant

Can you guide me what is the proper way to implement his migration, please?

1

There are 1 best solutions below

0
knittl On BEST ANSWER

If you want the duration between two instants, then you are looking for Duration#between(Instant, Instant):

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