Search code examples
groovy

Groovy not converting seconds to hours properly


I have an integer that is: 19045800

I tried different code:

def c = Calendar.instance
c.clear()
c.set(Calendar.SECOND, 19045800)
echo  c.format('HH:mm:ss').toString()
String timestamp = new GregorianCalendar( 0, 0, 0, 0, 0, 19045800, 0 ).time.format( 'HH:mm:ss' )
echo timestamp

Both return 10:30:00

19045800 seconds is supposed to be over 5000 hours. What am I doing wrong?


Solution

  • I'm not sure what you are looking for. But if your requirement is to calculate the number of hours, minutes, and the remainder of seconds for given seconds following code will work.

    def timeInSeconds = 19045800
    
    int hours = timeInSeconds/3600 
    int minutes = (timeInSeconds%3600)/60
    int seconds = ((timeInSeconds%3600)%60)
    
    println("Hours: " + hours)
    println("Minutes: " + minutes)
    println("Seconds: " + seconds)