2010-05-01 408 views
11

如何分鐘轉換爲天時間,並在Java分鐘(我們這裏有一個星期,7天)如何分鐘轉換爲天,小時,分鐘

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; 
} 

我試過,但它不工作

謝謝

回答

21

更簡短的方法。 (假設時間> = 0)

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

如果使用Java 6,TimeUnit枚舉可能會有用。例如:

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

這種靜態調用10天轉換成小時爲單位,並返回240您可以用納秒開始,隨着時光的結束時間單位玩。

其實TimeUnit可以從Java 5中獲得,但是在版本6中增加了更多的單元。

--EDIT-- 現在,我更瞭解您的問題,請使用除法和餘數方法,如同羅曼的回答一樣。我的提示僅適用於轉換爲單個時間單位。

+0

謝謝,但我唐諾如何使用這個,有沒有關於它的文檔 – Eddinho 2010-05-01 19:11:28

+0

@tuxou:有javadoc的 – 2010-05-03 08:52:55

15

如果你想自己做,就換個方法吧。

  1. 除以60 * 24(這將讓天數。)數
  2. 除以其餘由60(這會給你的小時數。)
  3. 的#剩餘2是分鐘數。
+0

,但我有更多的後來有一天,我的分鐘0結束開始在10080我將有136:30與這個算法 – Eddinho 2010-05-01 19:10:43

+3

羅曼的方法工作得很好,只要你在前兩種情況下都採用商和餘數。如果你有1600分鐘,除以(60 * 24),商數爲1,餘數爲160,然後除以60,商數爲2,餘數爲40.因此,1600分鐘等於1天,2小時,40分鐘。 – 2010-05-01 19:22:59

+0

@阿德里安洛佩茲 - 感謝您的解釋+1 – 2010-05-01 19:58:28

4

1)您的代碼是重複的。這是我認爲糟糕代碼的一個標誌。

2)除數不應隨天數而改變,因爲天數與一小時內的分鐘數無關。

除此之外,看看Romain Hippeau的方法,他告訴你如何去做。

2

答案是:

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

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



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

你怎麼看待這個代碼是什麼?

+0

總體上是一個很好的方法,但我的改進風格的建議是:a)不要使用單個字母的變量名b)避免預先聲明變量的值永遠不會被使用,它是更好的做例如「int hour =(time%(24 * 60))/ 60;」 – mikera 2010-08-03 15:38:15

+0

我不同意b點,Mikira建議你儘可能多地將代碼塞入一行。然而,如果你像Eddinho那樣將變量分成一小段步驟,那麼其他人就可以更容易地瀏覽代碼並查看發生了什麼。至於點a)這是一個好點。我也會偏離單字母變量,除非它們是暫時的,而我想起一個合適的名字。在這些情況下,我會描述變量的類型,例如s用於字符串,i用於int,dt用於DataTable等等。但是,Eddinho很好的解決方案。它的作品,優雅如此! – 2016-01-11 08:48:56

0

我使用此代碼。它也可以提供幫助。

private String getText(int minutes){ 

    int weeks = minutes/10080; 
    int aboveWeeks = minutes % 10080; 
    int days = aboveWeeks/1440; 
    int aboveDays = aboveWeeks % 1440; 
    int hours = aboveDays/60; 
    int aboveHours = aboveDays % 60; 
    int minute = aboveHours/60; 

    if(weeks > 0 && days > 0) { 
     if(weeks > 1 && days > 1){ 
      return weeks + " weeks " + days + " days before"; 
     } else { 
      return weeks + " weeks " + days + " day before"; 
     } 
    } else if (weeks > 0){ 
     if (weeks > 1){ 
      return weeks + " weeks before"; 
     } else { 
      return weeks + " week before"; 
     } 
    } else if(days > 0 && hours > 0){ 
     if(days > 1 && hours > 1){ 
      return days + " days " + hours + " hours before"; 
     } else { 
      return days + " days " + hours + " hour before"; 
     } 
    } else if(days > 0){ 
     if (days > 1){ 
      return days + " days before"; 
     } else { 
      return days + " day before"; 
     } 
    } else if(hours > 0 && minute > 0){ 
     if(hours > 1 && minute > 1){ 
      return hours + " hours " + minute + " minutes before"; 
     } else { 
      return hours + " hours " + minute + " minute before"; 
     } 
    } else if(hours > 0){ 
     if (hours > 1){ 
      return hours + " hours before"; 
     } else { 
      return hours + " hour before"; 
     } 
    } else { 
     if (minutes > 1){ 
      return minutes + " minutes before"; 
     } else { 
      return minutes + " minute before"; 
     } 
    } 
} 
0
class time{ 
    public static void main (String args[]){ 
     System.out.println("Hello"); 
     int duration=1500; 
     String testDuration = ""; 

     if(duration < 60){ 
      testDuration = duration + " minutes"; 
     } 
     else{ 

      if((duration/60)<24) 
      { 
       if((duration%60)==0){ 
        testDuration = (duration/60) + " hours"; 
       } 
       else{ 
      testDuration = (duration/60) + " hours," + (duration%60) + " minutes"; 
       } 
      } 
      else{ 

       if((duration%60)==0){ 
        if(((duration/60)%24)==0){ 
         testDuration = ((duration/24)/60) + " days,"; 

        } 
        else{ 
        testDuration = ((duration/24)/60) + " days," + (duration/60)%24 +"hours"; 
        } 
       } 
        else{ 
       testDuration = ((duration/24)/60) + " days," + (duration/60)%24 +"hours"+ (duration%60) + " minutes"; 
        } 
      } 
     } 

     System.out.println(testDuration); 
    } 
} 
相關問題