How to get the last working day of the month?

11

Is there a reliable holiday library in Brazil that we can integrate and get on the last business day of any month?

Anyway, if there is no such library, how could I get the last weekday of any month? (Formatted in a Calendar or a Date.)

Sample Code Required

Calendar hoje = Calendar.getInstance();
Calendar ultimoDiaUtil = getUltimoDiaUtil(hoje);
    
asked by anonymous 30.04.2014 / 15:27

2 answers

12
  

You can get the last day of the month by adding -1 day to a date that represents the first day of the following month. And if you want the last day of the week, just go back the days until you find a Friday (if the last day is not already). In at most two iterations you get the desired day.

It works as long as you do not consider holidays.

If you want to consider the holidays, map them and add one more condition in the loop by checking if the date is not a holiday, if it is a holiday, keep decreasing until the condition is not met and drops out of% / p>

Implementing the @Renan idea plus that part of the code that checks for 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;
    }
}

Return:

  

04/29/2014

Because in my code it said that today is a holiday, so it showed the date of yesterday.

For the comparison made in the code:

calendar.get(Calendar.DAY_OF_YEAR) == feriado.get(Calendar.DAY_OF_YEAR)

To make sure that the holiday year is set to the same year as the date you are going to compare, like this:

feriado.set(
    calendar.get(Calendar.YEAR), //ano de feriado igual ao ano de calendar
    Calendar.APRIL, //constante estática da classe Calendar
    30);

Because while returns an integer that says what day of that year, so if it is set April 30th of a year, it would make a difference of one day if you compare the date of a leap year with the same date as a year year non-leap.

To consider holidays whose date varies from year to year you should find out the account that is made and implement it for example in a calendar.get(Calendar.DAY_OF_YEAR) method and add it in ehFeriadoQueVaria() .

Editing for future reference:

Holidays schedule provided by Google in the format XML .

    
30.04.2014 / 15:59
-2
Calendar c = Calendar.getInstance();
    //alterar para o mes de abril
    c.set(Calendar.MONDAY, Calendar.APRIL);
    //pega o ultimo dia do mes de abril
    int ultimoDia =c.getActualMaximum(Calendar.DAY_OF_MONTH);
    
30.04.2014 / 20:04