Skip to content
Groovy 39 – How to Add 2 numbers as Hours and Minutes

Add Hours Function

Function Name: addHours

Returns: String

Parameters: hoursArray - List

       
        def hours = []  
        hours = hoursArray  
        def min_sum = 0  
        def h = 0  
        
        for (int i = 0; i < hours.size(); i++) {  
            def min = Integer.parseInt(hours[i].toString().substring(hours[i].toString().indexOf(".") + 1, hours[i].toString().length()))  
            def makeMinut = min  
            if (String.valueOf(min).length() == 1) {  
                makeMinut = min * 10  
            }  
            min_sum += makeMinut  
        }  
        
        for (int x = 0; x < hours.size(); x++) {  
            h += Integer.parseInt(hours[x].toString().substring(0, hours[x].toString().indexOf(".")))  
        }  
        
        int div = min_sum / 60  
        def mod = min_sum % 60  
        h = h + div  
        
        return h + "." + mod  
       
        

Usage of the function:

     
        addHours(hoursArray)