2011-08-16 9 views
9

avevo scritto una funzione per l'aggiunta di tempo, come indicato di seguitoHai bisogno di ottenere il tempo in un formato 24 ore, mentre l'aggiunta Tempo

private void Delay15Minute() { 
     String pkManifest = manifest.pkManifestNo; 
     manifest_helper = new manifest_helper(this); 
     cursor = manifest_helper.GetDeliveries(pkManifest); 
     cursor.moveToFirst(); 
     for (int i = 0; i < cursor.getCount(); i++) { 
      cursor.getString(cursor.getColumnIndex("PKDelivery")); 
      // String 
      // RevisedTime=cursor.getString(cursor.getColumnIndex("RevisedEstimatedDeliveryTime")); 
      String RevisedTime = "12:55"; 

      // get hour and minute from time string 
      StringTokenizer st1 = new StringTokenizer(RevisedTime, ":"); 
      int j = 0; 
      int[] val = new int[st1.countTokens()]; 
      // iterate through tokens 
      while (st1.hasMoreTokens()) { 
       val[j] = Integer.parseInt(st1.nextToken()); 
       j++; 
      } 
      // call time add method with current hour, minute and minutesToAdd, 
      // return added time as a string 
      String date = addTime(val[0], val[1], 15); 
      // Tioast the new time 
      Toast.makeText(this, "date is =" + date, Toast.LENGTH_SHORT).show(); 

     } 
      } 



public String addTime(int hour, int minute, int minutesToAdd) { 
     Calendar calendar = new GregorianCalendar(1990, 1, 1, hour, minute); 
     calendar.add(Calendar.MINUTE, minutesToAdd); 
     SimpleDateFormat sdf = new SimpleDateFormat("hh:mm"); 
     String date = sdf.format(calendar.getTime()); 

     return date; 
    } 

Mi è toccato l'oupt di questo come 01:10 12 ore fromat ...

ho bisogno di ottenere in formato 13:10 cioè formato 24 ore ..... Ti prego, aiutami

risposta

36

Hai usato hh nel vostro SimpleDateFormat modello. Questo è il formato di 12 ore. Utilizzare invece kk per fornire le ore del giorno in un formato di 24 ore. Vedi SimpleDateFormat.

+0

Grazie per il feedback ... mi Ite aiutano a trovare una soluzione –

+5

Se si utilizza ** ** kk otterrete risultati come ** 24: 30 ** ma se usi ** HH ** puoi ottenerlo come ** 00: 30 **. –

37

sufficiente creare l'istanza di Calendar e ottenere il tempo 24 ore per,

Calendar c = Calendar.getInstance(); 

int Hr24=c.get(Calendar.HOUR_OF_DAY); 
int Min=c.get(Calendar.MINUTE); 
+0

La tua risposta mi ha aiutato, grazie! –

+0

TU sei il salvatore ... anche dopo HH: mm non ha mostrato risultati! THANX – user3833732

Problemi correlati