0

In java i used TimeUtil package.

package org.core.java;

import java.util.concurrent.TimeUnit;

public class TimeUtil {

    public static void main(String[] args) {
        long millis = 20000000L; 
        String tt = String.format("%d days,%d hrs,%d min, %d sec",
                TimeUnit.MILLISECONDS.toDays(millis),
                TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis),
                TimeUnit.MILLISECONDS.toSeconds(millis) - 
                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
            );
        System.out.println(tt);
        String metrics[] = tt.split(",");
        String finalString = "";
        for(String  metric : metrics) {
            metric = metric.trim();
            Integer indexChar = Integer.valueOf(metric.substring(0,1));
            if(indexChar > 0) {
                finalString += metric+" ";
            }
        }
        System.out.println(String.valueOf(finalString));
    }

}

This returns 5 hrs 333 min 20 sec.

In this 333 should not come up.If min exceeded 60 then it will add +1 hrs.But in this its showing 5 hrs 333 min 20 sec.

Expected result approx 10 hrs : ... Please correct me .Am i wrong with the above code.

Thanks

Vijay
  • 344
  • 2
  • 11
  • Would you like to change the title and add tags for this question so it fits what you need? It will help anyone else who comes upon. Specify what needs to happen in your code and what's not working. – Unihedron Jun 24 '14 at 07:18
  • Why do you think converting the _same_ number to different units will decompose it?? – Boris the Spider Jun 24 '14 at 07:19
  • What i expected is 1000L ms returns 1 sec also if 100000L returns 1 min 40 sec.the same thing is for days,hours,minutes,seconds... – Vijay Jun 24 '14 at 07:24
  • The returned value is good, you're creating a string containing 20000000ms in days, 20000000ms in hours, 20000000ms in minutes and the seconds remaining after conversion in minutes. You have to do a little math, the same you did for seconds. Also have a look at [ISODateTimeFormat](http://www.joda.org/joda-time/apidocs/org/joda/time/format/ISODateTimeFormat.html) which seems to do pretty much what you want – Mathieu Le Tiec Jun 24 '14 at 07:28

0 Answers0