Last active
August 17, 2017 20:55
-
-
Save ioxua/347f91ab341cb7f9fbedecbd749e448b to your computer and use it in GitHub Desktop.
Format date according to the smallest measurement unit possible
This file contains 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
HashMap<Long, String> formatacoes = new LinkedHashMap<>(); | |
long segundo = 1000, | |
minuto = segundo * 60, | |
hora = minuto * 60, | |
dia = hora * 24, | |
semana = dia * 7, | |
mes = semana * 4, | |
ano = mes * 12; | |
formatacoes.put(ano, "{0,choice,1#1 year ago|1<{0} years ago}"); | |
formatacoes.put(mes, "{0,choice,1#1 month ago|1<{0} months ago}"); | |
formatacoes.put(semana, "{0,choice,1#1 week ago|1<{0} weeks ago}"); | |
formatacoes.put(dia, "{0,choice,1#1 day ago|1<{0} days ago}"); | |
formatacoes.put(hora, "{0,choice,1#1 hour ago|1<{0} hours ago}"); | |
formatacoes.put(minuto, "{0,choice,1#1 minute ago|1<{0} minutes ago}"); | |
formatacoes.put(segundo, "{0,choice,1#1 second ago|1<{0} seconds ago}"); | |
String approximatedFormat(long time) { | |
String unidade = ""; | |
long quantidade = 0; | |
for(Map.Entry entry : formatacoes.entrySet()) { | |
if(time >= (Long)entry.getKey()) { | |
unidade = context.getString( (Integer)entry.getValue() ); | |
quantidade = time / (Long)entry.getKey(); | |
break; | |
} | |
} | |
return MessageFormat.format(unidade, quantidade); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment