2011-08-16 14 views
9

que había escrito una función para la Adición de tiempo como se indica a continuaciónrecibir mi tiempo en un formato de 24 horas, mientras que la adición de Tiempo

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

estoy consiguiendo el oupt en esto como 1:10 hasta 12 horas fromat ...

tengo que conseguirlo en formato 13:10 es decir, formato de 24 horas ..... por favor, me ayudan a

Respuesta

36

utilizó hh en su patrón SimpleDateFormat. Ese es el formato de 12 horas. Use kk en su lugar, que le da las horas del día en un formato de 24 horas. Ver SimpleDateFormat.

+0

Gracias por los comentarios ... Ite me ayude a encontrar una solución –

+5

Si utiliza ** ** k obtendrá resultados como ** 24: 30 ** pero si usa ** HH ** puede obtenerlo como ** 00: 30 **. –

37

simplemente crear la instancia de Calendar y obtener el tiempo de 24 horas por,

Calendar c = Calendar.getInstance(); 

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

Tu respuesta me ayudó, ¡gracias! –

+0

USTED es el salvador ... incluso después de HH: ¡mm no mostró resultados! THANX – user3833732

Cuestiones relacionadas