Created
October 25, 2018 19:38
-
-
Save HugoSilvaF/e91fdea9b2c78385689bfd1f19d75525 to your computer and use it in GitHub Desktop.
Formatar o tempo em milisegundos para String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2018 Hugo Silva <[email protected]> | |
* | |
* 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 <[email protected]> | |
*/ | |
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 [email protected] | |
* @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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Como usar
Adicione o método em seu código ou baixe a classe e implemente-a no seu projeto.
Retorna String, o tempo já formatado.
Resultado