Last active
September 19, 2017 13:57
-
-
Save marcusedu/d3abe8b86bf66e51fdcdbcaf3d835ccf to your computer and use it in GitHub Desktop.
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
package datahora.util; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Locale; | |
/** | |
* Created by Marcus Eduardo - marcusedu7@gmail.com on 11/09/2017. | |
*/ | |
public class ResumoHorario { | |
private Calendar calendar; | |
private Locale locale; | |
private SimpleDateFormat simpleDataFormat; | |
public ResumoHorario() { | |
this(new Locale("pt", "BR")); | |
} | |
public ResumoHorario(Locale locale) { | |
this.locale = locale; | |
calendar = Calendar.getInstance(locale); | |
simpleDataFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm", this.locale); | |
} | |
public String getDiferencaEntreCalendars(Calendar de, Calendar para) { | |
String diferenca; | |
if (Math.abs(de.getTimeInMillis() - para.getTimeInMillis()) < 2000 * 60) { | |
diferenca = "Agora pouco"; | |
} else if (isHoje(de, para)) { | |
diferenca = String.format("Hoje às %s:%s", de.get(Calendar.HOUR_OF_DAY), de.get(Calendar.MINUTE)); | |
} else if (isMesmoMes(de, para) && Math.abs(de.get(Calendar.DAY_OF_MONTH) - para.get(Calendar.DAY_OF_MONTH)) == 1) { | |
diferenca = String.format("Ontem às %s:%s", de.get(Calendar.HOUR_OF_DAY), de.get(Calendar.MINUTE)); | |
} else if (isMesmoMes(de, para) | |
&& Math.abs(de.get(Calendar.DAY_OF_MONTH) - para.get(Calendar.DAY_OF_MONTH)) >= 2 | |
&& Math.abs(de.get(Calendar.DAY_OF_MONTH) - para.get(Calendar.DAY_OF_MONTH)) <= 7) { | |
diferenca = String.format("N%s %s às %s:%s" | |
, getDiaDaSemana(de).matches("Domingo|Sábado") ? "o" : "a" | |
, getDiaDaSemana(de) | |
, de.get(Calendar.HOUR_OF_DAY) | |
, de.get(Calendar.MINUTE)); | |
} else { | |
diferenca = simpleDataFormat.format(de.getTime()); | |
} | |
return diferenca; | |
} | |
private String getDiaDaSemana(Calendar de) { | |
switch (de.get(Calendar.DAY_OF_WEEK)) { | |
case Calendar.SUNDAY: | |
return "Domingo"; | |
case Calendar.MONDAY: | |
return "Segunda"; | |
case Calendar.THURSDAY: | |
return "Terça"; | |
case Calendar.WEDNESDAY: | |
return "Quarta"; | |
case Calendar.TUESDAY: | |
return "Quinta"; | |
case Calendar.FRIDAY: | |
return "Sexta"; | |
default: | |
return "Sábado"; | |
} | |
} | |
private boolean isHoje(Calendar de, Calendar para) { | |
return de.get(Calendar.DAY_OF_MONTH) == para.get(Calendar.DAY_OF_MONTH) && isMesmoMes(de, para); | |
} | |
private boolean isMesmoMes(Calendar de, Calendar para) { | |
return de.get(Calendar.MONTH) == para.get(Calendar.MONTH) && isMesmoAno(de, para); | |
} | |
private boolean isMesmoAno(Calendar de, Calendar para) { | |
return de.get(Calendar.YEAR) == para.get(Calendar.YEAR); | |
} | |
public String getDiferencaDeCalendarParaAgora(Calendar de) { | |
return getDiferencaEntreCalendars(de, Calendar.getInstance(locale)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment