-1

I have a number in minutes, for example:

Int totalminutes= 342

I want to dispaly it like this:

5h:42m:..s

I did a scala function to convert the minute to hour and return HH:MM:SS

scala> def convertSeconds(input:Int):String ={
     | val totalMinutes = input/60
     | val seconds = input%60
     | val minutes = totalMinutes%60
     | val hours   = totalMinutes%60
     |
     | return "%sh:%sm:%ss".format(hours,minutes,seconds)
     | }
convertSeconds: (input: Int)String

I did my tests:

scala> convertSeconds(120) // 120 minutes
res22: String = 2h:2m:0s  // why it add 2 minutes

scala> convertSeconds(60) // 60 minutes
res23: String = 1h:1m:0s  // adding 1 minute

scala> convertSeconds(36)  // 36 minutes
res24: String = 0h:0m:36s   // I want to return 0h:30m:06s

The solution is:

scala> def convertSeconds(input:Int):String ={
//val totalMinutes = input/60
//val seconds = input/60%60
val minutes = input%60
val hours   = input/60
return "%sh:%sm".format(hours,minutes)
}
convertSeconds: (input: Int)String

scala> convertSeconds(36)
res57: String = 0h:36m

scala> convertSeconds(120)
res58: String = 2h:0m

scala> convertSeconds(122)
res59: String = 2h:2m

scala> convertSeconds(2166)
res60: String = 36h:6m
vero
  • 1,005
  • 6
  • 16
  • 29
  • 3
    You calculate minutes and hours with the same formula, so you'll get the same number. – Don Branson Jul 18 '18 at 11:55
  • I edited my question thank you – vero Jul 18 '18 at 12:12
  • 3
    If a method converts `36` to `0h:36m`, it probably should be called `convertMinutes`... – Andrey Tyukin Jul 18 '18 at 12:49
  • @vero - You answered your question in the question. The desired approach on SO would be to self-answer the question. – Don Branson Jul 18 '18 at 15:15
  • 1
    I recommend you find some inspiration here: [Why can't I get a duration in minutes or hours in java.time?](https://stackoverflow.com/questions/24491243/why-cant-i-get-a-duration-in-minutes-or-hours-in-java-time) (the answer is you can) – Ole V.V. Jul 19 '18 at 09:49
  • Possible duplicate of [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. Jul 19 '18 at 09:58

3 Answers3

2

Your function is supposed to be like this and you pass seconds not minutes:

def convertSeconds(input:Int):String ={
       val hours   = input/3600
       val minutes = (input-hours*3600)/60
       val seconds = input%3600-minutes*60
       "%sh:%sm:%ss".format(hours,minutes,seconds)
     }

If you want to pass minutes, the function should be something like this, I named it convertMinutes, and your seconds will zero, and you can get in your prescribed display by this code:

def convertMinutes(input:Int):String ={
        val hours   = input/60
        val minutes = input-hours*60
        "%sh:%sm:%ss".format(hours,minutes,"..")
    }

In Scala REPL:

scala> convertSeconds(7523)
res4: String = 2h:5m:23s


scala> convertSeconds(9523)
res5: String = 2h:38m:43s

scala> convertSeconds(3724)
res6: String = 1h:2m:4s


scala> convertMinutes(342)
res10: String = 5h:42m:..s
RAGHHURAAMM
  • 1,099
  • 7
  • 15
1

In Java, trusting you to translate yourself:

    int totalminutes = 342;
    Duration dur = Duration.ofMinutes(totalminutes);
    String display = String.format("%dh:%02dm:%02ds",
            dur.toHours(), dur.toMinutesPart(), dur.toSecondsPart());
    System.out.println(display);

This prints:

5h:42m:00s

If had a number of seconds instead, use Duration.ofSeconds instead.

Don’t do the calculation yourself. It’s error-prone and it will require your reader to take some time to convince herself/himself that your calculation is correct. Instead let the library methods do the calculations, even when they seem trivial.

What went wrong in your code?

It’s been said already: You used

 val hours   = totalMinutes%60

This should have been a division:

 val hours   = totalMinutes / 60

It’s easier than you might expect to make such an error. For comparison you don’t that easily call toMinutesPart when you intended toHours, and if you still do, it’s easier to spot the bug.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

I am not sure if you need a separate function to achieve this. Scala already gives a library to do that

scala> val secs = 62
secs: Int = 62

scala> import scala.concurrent.duration._

scala> val inTime = Duration(secs,SECONDS)
inTime: scala.concurrent.duration.FiniteDuration = 62 seconds

scala> "%02d:%02d:%02d".format(inTime.toHours,inTime.toMinutes%60,inTime.toSeconds%60)
res8: String = 00:01:02

EDIT-1: What this does not handle is if you pass a number >= 86400 (total seconds in a day)

ForeverLearner
  • 1,901
  • 2
  • 28
  • 51