2012-01-18 18 views
8

Potrzebuję przekonwertować minutes (zdefiniowany jako liczba całkowita) do następującego formatu ciągu "hh: mm", zakładając, że startTime jest "00:00". Poniższy kod jest tym, co do tej pory miałem, ale nie działa poprawnie. Również nie bierze pod uwagę, że newTime należy przesunąć zgodnie z startTime. Czy jest jakieś inne rozwiązanie?Konwertuj liczby całkowite na ciąg "hh: mm"

String startTime = "00:00"; 
int minutes = 120; 
double time = minutes/60; 
String timeS = Double.toString(time); 
String[] hourMin = timeS.split("."); 
String h = hourMin[0]; 
String m = hourMin[1]; 
String newTime = "";  
newTime.concat(h+":"+m); 
+0

Użyj JodaTime. Sprawia, że ​​praca z datą/czasem faktycznie działa! – cdeszaq

+0

Czy mógłbyś podać przykład? –

+0

prawdopodobnie powinien dodać tag 'homework' –

Odpowiedz

6
String startTime = "00:00"; 
int minutes = 120; 
int h = minutes/60 + Integer.parseInt(startTime.substring(0,1)); 
int m = minutes % 60 + Integer.parseInt(startTime.substring(3,4)); 
String newtime = h+":"+m; 
+1

int h = minuty/60 + Integer.valueOf (startTime.substring (0,1)); –

+1

Jak mogę uzyskać wyjście "02:00"? Teraz jest "2: 0". –

+2

Możesz użyć String.format, np. String.format ("% 02d", godziny). – apines

1

Napisałem to ostatnio do konwersji sekund na godziny, minuty i sekundy; dostosuj go tak, jak chcesz (zamień "s", "m", "h" na dwukropki, pomiń sekundy itd.).

Edytuj: Naprawianie głupich błędów w moim kodzie po próbie poprawienia go w pośpiechu.

private static String getFormattedTime(int secs) { 
    // int secs = (int) Math.round((double) milliseconds/1000); // for millisecs arg instead of secs 
    if (secs < 60) 
     return secs + "s"; 
    else { 
     int mins = (int) secs/60; 
     int remainderSecs = secs - (mins * 60); 
     if (mins < 60) { 
      return (mins < 10 ? "0" : "") + mins + "m " 
        + (remainderSecs < 10 ? "0" : "") + remainderSecs + "s"; 
     } 
     else { 
      int hours = (int) mins/60; 
      int remainderMins = mins - (hours * 60); 
      return (hours < 10 ? "0" : "") + hours + "h " 
        + (remainderMins < 10 ? "0" : "") + remainderMins + "m " 
        + (remainderSecs < 10 ? "0" : "") + remainderSecs + "s"; 
     } 
    } 
} 
20

Proszę bardzo:

int min = 125; 

long hours = TimeUnit.MINUTES.toHours(120); 
long remainMinute = min - TimeUnit.HOURS.toMinutes(hours); 
String result = String.format("%02d", hours) + ":" 
        + String.format("%02d", remainMinute); 
System.out.println(result); 


/*Output: 02:05 */ 
+2

' TimeUnit' jest drogą do zrobienia, bez ręcznej konwersji. – matsev

+5

Można nawet skrócić go do: 'String.format ("% 02d:% 02d ", godzin, pozostanieMinutes);' – membersound

+0

, aby przekonwertować ciąg do minut http://stackoverflow.com/questions/8909075/convert-time-field -hm-into-integer-field-minutes-in-java – amorenew

0
String startTime = "00:00"; 
int minutes = 120; 

//--- counting 
int hour = minutes/60; 
minutes=minutes-hours*60; 
String m = Integer.toString(minutes); 
String h = Integer.toString(hours); 
newTime=h+":"+m; 
0
int ALLTIME_IN_MINUTES = 1444 
int hour = ALLTIME_IN_MINUTES /60; 
      int min = ALLTIME_IN_MINUTES %60; 


      String dateStr = hour+":"+min; 
      SimpleDateFormat curFormater = new SimpleDateFormat("H:m"); 
      Date dateObj = curFormater.parse(dateStr); 
      SimpleDateFormat postFormater = new SimpleDateFormat("HH:mm"); 

      String newDateStr = postFormater.format(dateObj); 

WYNIK: 24:04

-2
int minutes = 129; 
double hoursAndMinutes = (minutes/60) 
     + (double) minutes % 60/100; 
String hoursAndMinutesStr = new DecimalFormat("0.00").format(hoursAndMinutes).replace('.', ':'); 
System.out.println(hoursAndMinutesStr); 
+0

Chociaż technicznie wynik jest poprawny (ponieważ 60 <100), jest to zły sposób podejścia do tego typu zadań. Podział jest logiczny (godzina ma 60 minut), a nie matematyczny. Minuty nie są "ułamkami godziny". –

+1

Plus, proszę nie dodawać odpowiedzi na pytania z wieloma poprawnymi odpowiedziami, które zostały już zaakceptowane. –

0

Odpowiedź apines jest dobra, ale jeśli chcesz usunąć ostrzeżenie w Androidzie, dodaj "Locale":

int hours = 3; 
int minutes = 6; 
private static final String TIME_SEPARATOR = ":"; 
String.format(Locale.getDefault(), "%02d"+TIME_SEPARATOR+"%02d", hours, minutes); 
Powiązane problemy