Skip to content

Instantly share code, notes, and snippets.

@HugoSilvaF
Created October 25, 2018 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoSilvaF/e91fdea9b2c78385689bfd1f19d75525 to your computer and use it in GitHub Desktop.
Save HugoSilvaF/e91fdea9b2c78385689bfd1f19d75525 to your computer and use it in GitHub Desktop.
Formatar o tempo em milisegundos para String
/*
* Copyright (C) 2018 Hugo Silva <hugosilvaf2@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gmail.hugosilvaf2.timerformatter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* @author Hugo Silva <hugosilvaf2@gmail.com>
*/
public class TimerFormatter {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Convertendo segundos em milisegundos -> segundos * 100
System.out.println("fomated-> " + timerFormatter(125 * 1000));
}
/**
* Converter o tempo para milisegundos, se estiver em segundos multiplicar
* por 1000
*
*
* @author Hugo Silva hugosilvaf2@gmail.com
* @param time
* @return String com o tempo formatado
*/
public static String timerFormatter(long time) {
// Limite de cada
final int second = 60;
final int minute = 60;
final int hour = 24;
final int day = 30;
final int month = 12;
int seconds = 0, minutes = 0, hours = 0, days = 0, months = 0, years = 0;
StringBuilder builder = new StringBuilder();
seconds = (int) (time / 1000);
while (true) {
if (seconds >= second) {
seconds -= second;
minutes++;
continue;
}
if (minutes >= minute) {
minutes -= minute;
hours++;
continue;
}
if (hours >= hour) {
hours -= hour;
days++;
continue;
}
if (days >= day) {
days -= day;
months++;
continue;
}
if (months >= month) {
months -= month;
years++;
continue;
}
break;
}
if (years >= 1) {
builder.append(years + " ano(s)");
}
if (months >= 1) {
builder.append(" " + months + " mes(es)");
}
if (days >= 1) {
builder.append(" " + days + " dia(s)");
}
if (hours >= 1) {
builder.append(" " + hours + " hora(s)");
}
if (minutes >= 1) {
builder.append(" " + minutes + " minute(s)");
}
if (seconds >= 1) {
builder.append(" " + seconds + " segundo(s)");
}
return builder.toString();
}
}
@HugoSilvaF
Copy link
Author

HugoSilvaF commented Oct 25, 2018

Como usar

Adicione o método em seu código ou baixe a classe e implemente-a no seu projeto.

        System.out.println("-> " + timerFormatter(125 * 1000));
        System.out.println("-> " + timerFormatter(999994900));
        System.out.println("-> " + timerFormatter(System.currentTimeMillis()));

Retorna String, o tempo já formatado.

Resultado

->  2 minute(s) 5 segundo(s)
->  11 dia(s) 13 hora(s) 46 minute(s) 34 segundo(s)
-> 49 ano(s) 6 mes(es) 9 dia(s) 20 hora(s) 6 minute(s) 58 segundo(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment