I have a function that doesn't crash, but it takes a long time and shows me a lot of errors (which according to me, they aren't).
Must be one of: Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, Calendar.SATURDAY
These lines mark me with an error:
Calendar.JANUARY, Calendar.MAY, Calendar.SEPTEMBER, Calendar.DECEMBER.
cal2.get(Calendar.DATE)==1, cal2.get(Calendar.DATE)==18, cal2.get(Calendar.DATE)==19, cal2.get(Calendar.DATE)==25.
This is my code:
// calcula los dias que faltan para que venza el ticket
public Integer getDiasLaborales () {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date fechaActual = sdf.parse(sdf.format(Calendar.getInstance().getTime()));
Date fechaTermino = sdf.parse(fecha_termino);
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(fechaTermino);
cal2.setTime(fechaActual);
float dias = (float)(fechaTermino.getTime() - fechaActual.getTime()) / 86400000; // 1 dia = 1000*60*60*24*1
float numberOfDays = Math.abs(dias) - Math.abs((int) dias) - 1; // valor absoluto
if(dias < 0) {
while (cal1.before(cal2)) {
if ((cal1.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) // Los Domingos
&&!(((cal1.get(Calendar.DATE) == 1) &&(cal1.get(Calendar.MONTH) == Calendar.JANUARY)) // Año Nuevo
&&((cal1.get(Calendar.DATE) == 1) &&(cal1.get(Calendar.MONTH) == Calendar.MAY) // Día Nacional del Trabajo
&&((cal1.get(Calendar.DATE) == 18) &&(cal1.get(Calendar.MONTH) == Calendar.SEPTEMBER)) // Independencia Nacional
&&((cal1.get(Calendar.DATE) == 19) && (cal1.get(Calendar.MONTH) == Calendar.SEPTEMBER)) // Día de las Glorias del Ejército
&&((cal1.get(Calendar.DATE) == 25) &&(cal1.get(Calendar.MONTH) == Calendar.DECEMBER)) // Navidad
)){
numberOfDays++;
}
cal1.add(Calendar.DATE, 1);
}
numberOfDays = numberOfDays * -1;
} else {
while (cal2.before(cal1)) {
if ((cal2.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) // Los Domingos
&& !(((cal2.get(Calendar.DATE) == 1) &&(cal2.get(Calendar.MONTH) == Calendar.JANUARY)) // Año Nuevo
&& ((cal2.get(Calendar.DATE) == 1) && (cal2.get(Calendar.MONTH) == Calendar.MAY)) // Día Nacional del Trabajo
&& ((cal2.get(Calendar.DATE) == 18) && (cal2.get(Calendar.MONTH) == Calendar.SEPTEMBER)) // Independencia Nacional
&& ((cal2.get(Calendar.DATE) == 19) && (cal2.get(Calendar.MONTH) == Calendar.SEPTEMBER)) // Día de las Glorias del Ejército
&& ((cal2.get(Calendar.DATE) == 25) && (cal2.get(Calendar.MONTH) == Calendar.DECEMBER)) // Navidad
)){
numberOfDays++;
}
cal2.add(Calendar.DATE,1);
}
}
//Log.d(TAG, String.format("%s : %.2f ---> %.2f", sdf.format(fechaTermino), dias, numberOfDays));
//Log.d(TAG, dias+" ---> "+numberOfDays+"\n\n");
return numberOfDays > 0 ? (int)Math.ceil(numberOfDays) : (int)Math.floor(numberOfDays); //ceil: redondea arriba cualquier decimal y floor: quita el decimal y lo aproxima para abajo
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
What is causing this error and how can I solve it?
Count business days Java
Example that returns the number of working days from Monday to Friday, not taking into account Saturdays, Sundays and holidays for example January 1, December 25 which we will add to a list of non-working dates, and obtain the days working days given in a range of dates, start date and end date.
See full example here