views:

355

answers:

5

hi

how to convert minutes into days hours and minutes in java ( we have a week here , 7 days )

 public String timeConvert(int time){
   String t = "";

   int h = 00;
   int m = 00;

  // h= (int) (time / 60);
  // m = (int) (time % 60);

  // if(h>=24) h=00;

   if((time>=0) && (time<=24*60)){
      h= (int) (time / 60);
      m = (int) (time % 60);
   }else if((time>24*60) && (time<=24*60*2)){
       h= (int) (time / (1440));
      m = (int) (time % (1440));
   }else if((time>24*60*2) && (time<=24*60*3)){
       h= (int) (time / (2880));
      m = (int) (time % (2880));
   }else if((time>24*60*3) && (time<=24*60*4)){
       h= (int) (time / (2880*2));
      m = (int) (time % (2880*2));
   }else if((time>24*60*4) && (time<=24*60*5)){
       h= (int) (time / (2880*3));
      m = (int) (time % (2880*3));
   }else if((time>24*60*5) && (time<=24*60*6)){
       h= (int) (time / (2880*4));
      m = (int) (time % (2880*4));
   }else if((time>24*60*6) && (time<=24*60*7)){
       h= (int) (time / (2880*5));
      m = (int) (time % (2880*5));
   }

   t =h+":"+m ;
   return t;
 }

I tried this but it dont work

thanks

+3  A: 

If you use Java 6, TimeUnit enum can be useful. For example:

TimeUnit.HOURS.convert(10, TimeUnit.DAYS)

This static call converts 10 days into hour units, and returns 240. You can play with time units starting from NANOSECONDS and ending with DAYS.

Actually TimeUnit is available from Java 5, but in version 6 more units were added.

--EDIT-- Now that I understand better your question, use the division and remainder approach as in the response of Romain. My tip is useful only for conversion to a single time unit.

Eyal Schneider
thanks, but i donno how to use this, there is no docs about it
tuxou
@tuxou : there is the javadoc
Valentin Rocher
+6  A: 

If you want to do this yourself go the other way.
1) Divide the number by 60 * 24 (That will get the number of days)
2) Divide the remainder by 60 (That will give you number of hours)
3) The remainder of #2 is the number of minutes.

Romain Hippeau
but i have more then one day my minutes begin on 0 ends on 10080 i will have like 136:30 with this algorothm
tuxou
Romain's method works just fine so long as you take both the quotient and remainder in each of the first two cases. If you have 1600 minutes and divide by (60 * 24), the quotient is 1 and the remainder is 160 which you then divide by 60. The quotient of that is 2 and the remainder is 40. Thus 1600 minutes equals 1 day, 2 hours, and 40 minutes.
Adrian Lopez
@Adrian Lopez - Thanks for the explanation +1
Romain Hippeau
A: 

1) Your code is horribly repetitive. This is a major sign of bad code.

2) The number of days involved has nothing to do with the number of minutes in an hour, thus your divisor shouldn't be changing with the number of days.

Beyond that, look at Romain Hippeau's approach, he told you how to do it.

Loren Pechtel
+1  A: 

and the answer is :

 public String timeConvert(int time){
   String t = "";

  int j = 0;
  int h = 0;
  int m = 0;

   j = time/(24*60);
   h= (time%(24*60)) / 60;
   m = (time%(24*60)) % 60;



   t =j + ":" + h + ":" + m;
   return t;
 }

what do you think about this code?

tuxou
Nice approach overall , but my suggestions to improve style would be: a) Don't use single letter variable names b) Avoid pre-declaring variables with values that never get used, it's better to do e.g. "int hour = (time%(24*60)) / 60;"
mikera
+1  A: 

A shorter way. (Assumes time >= 0)

 public String timeConvert(int time) { 
   return time/24/60 + ":" + time/60%24 + ':' + time%60;
 }
Peter Lawrey