2013-01-01 15 views
6

Mam dwie zmienne łańcuchowe, takie jak StartTime i EndTime. Muszę obliczyć TotalTime przez odjęcie EndTime z StartTime.Oblicz różnicę między dwoma razy w systemie Android

format StartTime i EndTime jest jak następuje:

StartTime = "08:00 AM"; 
EndTime = "04:00 PM"; 

totalTime godzin i Mins Format. Jak obliczyć to w systemie Android?

+0

Klasa czas jest przydatna dla Android: http://stackoverflow.com/a/12739537/693752 – Snicolas

Odpowiedz

27

Spróbuj poniżej kodu.

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); 
date1 = simpleDateFormat.parse("08:00 AM"); 
date2 = simpleDateFormat.parse("04:00 PM"); 

long difference = date2.getTime() - date1.getTime(); 
days = (int) (difference/(1000*60*60*24)); 
hours = (int) ((difference - (1000*60*60*24*days))/(1000*60*60)); 
min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours))/(1000*60); 
hours = (hours < 0 ? -hours : hours); 
Log.i("======= Hours"," :: "+hours); 

Wyjście - Godziny :: 8

+0

Hej dziękuję u, różnica jest zawsze ujemna, mój czas rozpoczęcia to 12:30, a godzina zakończenia to 13:00. nadałem mu format "HH: mm a", a różnica jest nadal ujemna. Czas zakończenia Althogh jest dłuższy niż czas rozpoczęcia. – Munazza

+0

Czy muszę podać datę również z napisem? – Munazza

+0

użyj 01:00 PM zamiast 13:00 i spróbuj co się stało. –

3

Wystarczy popatrzeć na DateFormat, można go używać do analizowania swoich strun z metody parse (String source) i można łatwo manipulować dwoma datami obiekt, aby uzyskać to, co chcesz.

DateFormat df = DateFormat.getInstance(); 
Date date1 = df.parse(string1); 
Date date2 = df.parse(string2); 
long difference = date1.getTime() - date2.getTime(); 

days = (int) (difference/(1000*60*60*24)); 
hours = (int) ((difference - (1000*60*60*24*days))/(1000*60*60)); 
min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours))/(1000*60); 

String diffHours = df.format(hours); 

Dla różnicy dat

Date myDate = new Date(difference); 

pokazać Data:

String diff = df.format(myDate); 
0

Spróbuj to ....

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); 

    try { 
     date1 = simpleDateFormat.parse("08:00 AM"); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    try { 
     date2 = simpleDateFormat.parse("04:00 PM"); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    long difference = date2.getTime() - date1.getTime(); 
    int days = (int) (difference/(1000 * 60 * 60 * 24)); 
    int hours = (int) ((difference - (1000 * 60 * 60 * 24 * days))/(1000 * 60 * 60)); 
    int min = (int) (difference - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours)) 
      /(1000 * 60); 
    hours = (hours < 0 ? -hours : hours); 
    Log.i("======= Hours", " :: " + hours); 
2

Uwaga: Poprawione kod jak poniżej które zapewniają Chirag Raval, ponieważ w kodzie, który Chirag str rovided miał pewne problemy, gdy próbujemy znaleźć czas od 22:00 do 07:00.

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); 
Date startDate = simpleDateFormat.parse("22:00"); 
Date endDate = simpleDateFormat.parse("07:00"); 

long difference = endDate.getTime() - startDate.getTime(); 
if(difference<0) 
{ 
    Date dateMax = simpleDateFormat.parse("24:00"); 
    Date dateMin = simpleDateFormat.parse("00:00"); 
    difference=(dateMax.getTime() -startDate.getTime())+(endDate.getTime()-dateMin.getTime()); 
} 
int days = (int) (difference/(1000*60*60*24)); 
int hours = (int) ((difference - (1000*60*60*24*days))/(1000*60*60)); 
int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours))/(1000*60); 
Log.i("log_tag","Hours: "+hours+", Mins: "+min); 

Wynik będzie: Godziny: 9 min: 0

0
Try simple piece of code using For 24 hour time 
StartTime = "10:00"; 
EndTime = "13:00"; 
here starthour=10 and end hour=13 
if(TextUtils.isEmpty(txtDate.getText().toString())||TextUtils.isEmpty(txtDate1.getText().toString())||TextUtils.isEmpty(txtTime.getText().toString())||TextUtils.isEmpty(txtTime1.getText().toString())) 
    { 
     Toast.makeText(getApplicationContext(), "Date/Time fields cannot be blank", Toast.LENGTH_SHORT).show(); 
    } 
    else { 
     if (starthour > endhour) { 
      Toast.makeText(getApplicationContext(), "Start Time Should Be Less Than End Time", Toast.LENGTH_SHORT).show(); 
     } else if (starthour == endhour) { 
      if (startmin > endmin) { 
       Toast.makeText(getApplicationContext(), "Start Time Should Be Less Than End Time", Toast.LENGTH_SHORT).show(); 
      } 
      else{ 
       tvalid = "True"; 
      } 
     } else { 
      // Toast.makeText(getApplicationContext(),"Sucess"+(endhour-starthour)+(endmin-startmin),Toast.LENGTH_SHORT).show(); 
      tvalid = "True"; 
     } 
    } 
same for date also 
Powiązane problemy