You can get the last day of the month by adding -1 day to a date representing the first day of the following month. And if you want the last day of the week, just go back to the days until you find a Friday (if the last day is no longer). In a maximum of two iterations you get the desired day.
It works as long as you don’t consider the holidays.
If you want to consider the holidays, simply blot them out and add one more condition in the loop by checking if the date is not a holiday, if it is a holiday continue to decrease until the condition is not met and falls outside the while
.
Implementing @Renan’s idea plus the part of code that checks whether it’s a holiday:
public static void main(String[] args) {
DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/YYYY");
Calendar hoje = Calendar.getInstance();
Calendar ultimoDiaUtilDoMes = getUltimoDiaUtilDoMes(hoje);
System.out.println(DATE_FORMAT.format(ultimoDiaUtilDoMes.getTime()));
}
public static Calendar getUltimoDiaUtilDoMes(Calendar calendar) {
//muda a data da variável para o último dia do mês
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
//enquanto for sábado, domingo ou feriado
while(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
ehFeriado(calendar)) {
//decrementa a data em um dia
calendar.add(Calendar.DATE, -1);
}
return calendar;
}
public static boolean ehFeriado(Calendar calendar) {
Calendar feriado = Calendar.getInstance();
//considerando 30 de abril como feriado
feriado.set(calendar.get(Calendar.YEAR), Calendar.APRIL, 30);
if(calendar.get(Calendar.DAY_OF_YEAR) == feriado.get(Calendar.DAY_OF_YEAR)) {
return true;
}
else {
return false;
}
}
Returns:
29/04/2014
'Cause my code says today’s a holiday, so it showed yesterday’s date.
For the comparison made in the code:
calendar.get(Calendar.DAY_OF_YEAR) == feriado.get(Calendar.DAY_OF_YEAR)
working out it is necessary that the holiday year be set as the same year of the date it will compare, so:
feriado.set(
calendar.get(Calendar.YEAR), //ano de feriado igual ao ano de calendar
Calendar.APRIL, //constante estática da classe Calendar
30);
for the calendar.get(Calendar.DAY_OF_YEAR)
returns a integer that says what is the day of that year, so if it is set April 30 of a year any would give difference of a day if compare the date of a leap year with the same date of a non-leap year.
To consider the holidays whose date varies from year to year one must discover the account that is made and implement it for example in a method ehFeriadoQueVaria()
and add it to while()
.
Edition for future reference:
Schedule of Holidays made available by Google in format XML.
You can get the last day of the month by adding -1 day to a date representing the first day of the following month. And if you want the last day of the week, just go back to the days until you find a Friday (if the last day is no longer). In a maximum of two iterations you get the desired day.
– Oralista de Sistemas
It should be simple to keep a configuration file (an XML maybe) with national and state holidays. In the interaction @Renan mentioned, just ignore the dates that fall on a configured holiday. Reliable sources of holiday information are 1 and 2.
– Luiz Vieira
@Renan your solution is very good, since the author does not need to consider the holidays, because it does not implement it?
– Math
@Math because I am a zero left in Java and do not know the date and time types of the framework. If it were . NET or Javascript I could help more. If anyone wants to give a full answer below with my idea, feel free :)
– Oralista de Sistemas