Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Required type: TemporalUnit
Provided: Instant

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

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading