Skip to content

Instantly share code, notes, and snippets.

@victorvaz
Last active October 30, 2015 02:45
Show Gist options
  • Save victorvaz/566975e68c66b5e56c7f to your computer and use it in GitHub Desktop.
Save victorvaz/566975e68c66b5e56c7f to your computer and use it in GitHub Desktop.
/**
* Classe para funções de tratamento de data.
*/
public class Data
{
/**
* Função para formatar o tempo em segundos para HH:MM:SS
* @param tempoSegundos Tempo gasto em segundos
* @return String Tempo formatado
*/
public String formataTempo(long tempoSegundos)
{
long segundos = tempoSegundos % 60;
tempoSegundos /= 60;
long minutos = tempoSegundos % 60;
tempoSegundos /= 60;
long horas = tempoSegundos % 24;
return strzero(horas) + ":" + strzero(minutos) + ":" + strzero(segundos);
}
/**
* Função para formatar com zero.
* @param num
* @return
*/
private String strzero(long num)
{
if (num < 10)
return "0" + String.valueOf(num);
else
return String.valueOf(num);
}
}
// Unix time do momento que iniciou o processo:
long unixTimeInicio;
// Unix time do momento que finalizou o processo:
long unixTimeFim;
Data cData = new Data();
tempoGasto = cData.formataTempo(unixTimeFim - unixTimeInicio);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment