Convert milliseconds to hh: mm format in java

6

I'm doing an Android game with a java engine where it has a Timer that stays all the time running during the Game ... This timer is a millisecond counter and is added in a long.

In the end I need to convert this long to a format in hours and minutes (hh: mm), how do I do this in java?

    
asked by anonymous 29.04.2014 / 07:47

2 answers

11

1) Quick response:

String.format( "%03d:%02d", ms / 3600000, ( ms / 60000 ) % 60 );


2) "Traditional" mathematical solution:


Calculating the fields:

Assuming ms is the millisecond variable:

   segundos = ( ms / 1000 ) % 60;      // se não precisar de segundos, basta remover esta linha.
   minutos  = ( ms / 60000 ) % 60;     // 60000   = 60 * 1000
   horas    = ms / 3600000;            // 3600000 = 60 * 60 * 1000
   System.out.println( String.format( "%03d:%02d", horas, minutos ) );
  • The % operator is the module: A % B returns the remainder of the A by B ;

  • The module was used in segundos and minutos to be restricted from 0 to 59;

  • In% with_%, no module has been applied to allow outputs such as horas , for example.

If you want, you can add a new line to calculate the days using the same logic:

   segundos = ( ms / 1000 ) % 60;  
   minutos  = ( ms / 60000 ) % 60;     // 60000    = 60 * 1000
   horas    = ( ms / 3600000 ) % 24;   // 3600000  = 60 * 60 * 1000
   dias     = ms / 86400000            // 86400000 = 24 * 60 * 60 * 1000


Formatting Output:

For output, we use 107:38 with the mask String.format() , which returns us 3 boxes filled by zero left on the first parameter, and two for the second parameter:

   107:38

If you want to add the seconds to the counter, just update "%03d:%02d" :

   segundos = ( ms / 1000 ) % 60;
   minutos  = ( ms / 60000 ) % 60;     // 60000   = 60 * 1000
   horas    = ms / 3600000;            // 3600000 = 60 * 60 * 1000
   System.out.println( String.format( "%03d:%02d:%02d", horas, minutos,segundos ) );

Output:

   107:38:13


3) Solution using java.util.concurrent.TimeUnit

If the String.format class is available (Android API 9+), this code can be used (assuming java.util.concurrent.TimeUnit is the millisecond variable):

   String.format("%03d:%02d", TimeUnit.MILLISECONDS.toHours( ms ),
                              TimeUnit.MILLISECONDS.toMinutes( ms ) % 60 );

I just mentioned that the class exists. It seems an exaggeration to me.

    
29.04.2014 / 08:18
5

Unlike solutions using mathematical calculations, I propose one that, while simple, can be easily adapted to other formats.

The following method takes the time in milliseconds and uses the Calendar class to calculate the milliseconds and Java default formatting to return the desired format:

public static String msToHourSecond( int ms ) {
  Calendar c = Calendar.getInstance();
  c.set( Calendar.HOUR , 0 );
  c.set( Calendar.MINUTE , 0 );
  c.set( Calendar.SECOND , 0 );
  c.set( Calendar.MILLISECOND , 0 );
  c.add( Calendar.MILLISECOND , ms );
  return new SimpleDateFormat( "HH:mm" ).format( c.getTime() );
}

Examples of use:

System.out.println( msToHourSecond( 1000 * 60 * 11 ) ); // 11 minutos
System.out.println( msToHourSecond( 1000 * 60 * ( 15 + 60 * 2 ) ) ); // 2 horas e 15 minutos

Output:

  

  02:15

    
29.04.2014 / 16:44