justanothertekguy
justanothertekguy

Reputation: 183

How to compute duration between 2 timestamp variables?

I want to compute the duration between 2 timestamp variables and output in HH:MM:SS format

val start_time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now)
val sourceFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val start_time_dt = sourceFormat.parse(start_time)
val end_time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now)
val end_time_dt = sourceFormat.parse(end_time)

I am trying to do this:

Duration.between(end_time_dt, start_time_dt)

but I am getting the below error:

found   : java.util.Date
required: java.time.temporal.Temporal

Upvotes: 0

Views: 384

Answers (5)

Anonymous
Anonymous

Reputation: 86130

java.time.Instant

The LocalDateTime class does not define a point in time so is the wrong class to use for them. Using it will occasionally give you wrong durations. A good class to use is Instant.

    Instant start = Instant.now();
    TimeUnit.SECONDS.sleep(10);
    Instant end = Instant.now();
    Duration elapsedTime = Duration.between(start, end);
    
    System.out.println(elapsedTime);
    System.out.format("%02d:%02d:%02d%n", elapsedTime.toHours(),
            elapsedTime.toMinutesPart(), elapsedTime.toSecondsPart());

Example output:

PT10.002595S
00:00:10

The toXxxPart methods of Duration that I am using were introduced in Java 9.

Upvotes: 1

Michael Gantman
Michael Gantman

Reputation: 7792

Method Duration.between() does not accept Timestamp as an argument. All you need to do is:

Duration.between(end_time_dt.toInstant(), start_time_dt.toInstant());

Upvotes: 1

justanothertekguy
justanothertekguy

Reputation: 183

based on the comments from Ivan Kurchenko and jwh below is what I have put together! Not sure if there is any other better way to get the duration in the HH:MM:SS format

import java.time._
import java.time.format._

val sourceFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

val start_time = sourceFormat.format(LocalDateTime.now)
val start_time_dt = LocalDateTime.parse(start_time, sourceFormat)
Thread.sleep(10000)
val end_time = sourceFormat.format(LocalDateTime.now)
val end_time_dt = LocalDateTime.parse(end_time, sourceFormat)
val duration = Duration.between(start_time_dt, end_time_dt)
val seconds = duration.getSeconds()
val time_sec = seconds % 60
val time_min = (seconds/60) % 60
val time_hour = (seconds/60)/ 60

val dur = f"$time_hour%02d:$time_min%02d:$time_sec%02d"

Upvotes: 0

jwvh
jwvh

Reputation: 51271

You're mixing the old java.util.Date with the newer java.time.LocalDateTime.

import java.time.{LocalDateTime, Duration}

val start = LocalDateTime.now()
val end   = LocalDateTime.now()
val dur   = Duration.between(start, end)

dur.getNano()     //res0: Int = 390318000
dur.getSeconds()  //res1: Long = 0
dur.isZero()      //res2: Boolean = false

You should also note that you only need the Formatter when going from/to specific text representations of your dates and times. Since your posted code example neither reads from a date-time String, nor prints out a date-time String, I didn't include any formatting here.

Upvotes: 2

Ivan Kurchenko
Ivan Kurchenko

Reputation: 4063

Next should work for you - bottom line: if you are working with java.time package don't use nothing outside of it, like java.util.Date:

import java.time._
import java.time.format._

val sourceFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")

val start_time = sourceFormat.format(LocalDateTime.now)
val start_time_dt = LocalDateTime.parse(start_time, sourceFormat)

val end_time = sourceFormat.format(LocalDateTime.now)
val end_time_dt = LocalDateTime.parse(end_time, sourceFormat)

Duration.between(end_time_dt, start_time_dt)

Which produces result: PT0S: java.time.Duration

Scastie: https://scastie.scala-lang.org/o4itYdPrRdCNE5bosJEOfw

Upvotes: 1

Related Questions