Groovy web console

subscribe to the feed Subscribe
to this
site
duration (via #groovywebconsole)
tweet this snippet Tweet
this
script

duration

Published 4 months ago by Shane
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
String durationInSeconds='432000'
long durationInLong = 0 
        try {
            durationInLong = Long.parseLong(durationInSeconds);
        } catch(NumberFormatException nfe) {
            // null or any other failure.
        }
        TimeDuration durationString = TimeCategory.minus(new Date(durationInLong * 1000l), new Date(0))
        //because we have no milliseconds, remove that nasty ".000" part
        String returnDuration
        if(durationString?.months==0 && durationString?.days==0 && durationString?.hours==0){
            returnDuration =  durationString.minutes + "m"
        }        
            else if(durationString?.months==0 && durationString?.days==0){
                returnDuration =  durationString.hours + "h " + durationString.minutes + "m"
        }
            else if(durationString?.months==0 ){
                returnDuration =  durationString.days + "d " + durationString.hours + "h " + durationString.minutes + "m"
            }else{
                returnDuration =  durationString.months + "m " + durationString.days + "d " + durationString.hours + "h " + durationString.minutes + "m"
            }
return returnDuration