0

Hello everyone I'm trying to make a small timer that will display how many minutes and seconds are left until a certain time. I want to do this using the difference between the present time and the time to which the countdown is running, but I can't figure out how to do it. It is necessary to output the time in the format "hh:mm". That is, if now, for example, "13:27:28", and the desired time is "14:00:00", then the final result should be "32:32". And is it possible to compare time somehow? Check whether the present time is greater or less than the specified one.

import java.time.LocalTime
import java.time.format.DateTimeFormatter

fun main(args: Array<String>) {
    println(calculateTime("13:27:28", "14:00:00"))
}

private fun calculateTime(from: String, to: String): String {
    val formatter = DateTimeFormatter.ofPattern("hh:mm:ss")
    val time1 = LocalTime.parse(from, formatter)
    val time2 = LocalTime.parse(to, formatter)
    return time1.toString() + time2.toString()
}
  
Tsypk
  • 1
  • 1
  • Is [`Duration.between`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/time/Duration.html#between(java.time.temporal.Temporal,java.time.temporal.Temporal)) useful? – dan1st Nov 29 '22 at 11:51
  • 2
    Does this answer your question? [How do I get difference between two dates in android?, tried every thing and post](https://stackoverflow.com/questions/10690370/how-do-i-get-difference-between-two-dates-in-android-tried-every-thing-and-pos) – Robin Nov 29 '22 at 12:16
  • Have you checked this ? https://stackoverflow.com/questions/10690370/how-do-i-get-difference-between-two-dates-in-android-tried-every-thing-and-pos – Stephan John Nov 29 '22 at 12:25

1 Answers1

-1

Try Stopwatch from Apache Commons

val stopWatch = StopWatch().apply { start() }
...
stopWatch.stop()
println("Completed after $stopWatch")

and you get something like this: "Completed after 00:00:18.832"

AndrewL
  • 2,034
  • 18
  • 18