0

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

5 Answers5

2

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.

jwvh
  • 50,871
  • 7
  • 38
  • 64
1

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

Ivan Kurchenko
  • 4,043
  • 1
  • 11
  • 28
1

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());
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
1

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • thanks I am using Java 8...I used this `val dur = "%02d:%02d:%02d".format(duration.toHours(), duration.toMinutes(), duration.getSeconds())` but the time is coming to `00:28:1713` would the seconds would come in that format (1713)? – justanothertekguy Apr 14 '21 at 07:33
  • 1
    There are some options, @justanothertekguy. See for instance the second half of [my answer here](https://stackoverflow.com/a/52699793/5772882) or the answers under [How to format a duration in java? (e.g format H:MM:SS)](https://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss). – Ole V.V. Apr 14 '21 at 07:55
0

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"
  • 1
    Why format and parse back? That’s redundant and better skipped. If using on Java 9 and later, the `Duration` can also directly give you the hours, minutes and seconds: use `toHours()`, `toMinutesPart()` and `toSecondsPar()`. – Ole V.V. Apr 14 '21 at 04:31