IDEMPIERE-2072 get number of non business days between 2 dates / based on patch from Nicolas Micoud (nmicoud)

This commit is contained in:
Carlos Ruiz 2014-07-23 12:45:10 -05:00
parent 10ce8aa22e
commit 7bb8143f15
1 changed files with 50 additions and 0 deletions

View File

@ -20,6 +20,9 @@ import java.sql.Timestamp;
import java.util.BitSet; import java.util.BitSet;
import java.util.Calendar; import java.util.Calendar;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.List;
import org.compiere.model.MCountry;
/** /**
@ -829,4 +832,51 @@ public class TimeUtil
return retValue; return retValue;
} }
/** Returns number of non business days between 2 dates */
public static int getBusinessDaysBetween(Timestamp startDate, Timestamp endDate, int clientID, String trxName)
{
int retValue = 0;
if (startDate.equals(endDate))
return 0;
boolean negative = false;
if (endDate.before(startDate)) {
negative = true;
Timestamp temp = startDate;
startDate = endDate;
endDate = temp;
}
final String sql = "SELECT Date1 FROM C_NonBusinessDay WHERE AD_Client_ID=? AND Date1 BETWEEN ? AND ? AND COALESCE(C_Country_ID,0) IN (0, ?)";
List<Object> nbd = DB.getSQLValueObjectsEx(trxName, sql, clientID, startDate, endDate, MCountry.getDefault(Env.getCtx()).getC_Country_ID());
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(startDate);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
GregorianCalendar calEnd = new GregorianCalendar();
calEnd.setTime(endDate);
calEnd.set(Calendar.HOUR_OF_DAY, 0);
calEnd.set(Calendar.MINUTE, 0);
calEnd.set(Calendar.SECOND, 0);
calEnd.set(Calendar.MILLISECOND, 0);
while (cal.before(calEnd) || cal.equals(calEnd)) {
if (!nbd.contains(new Timestamp(cal.getTimeInMillis()))) {
if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
retValue++;
}
}
cal.add(Calendar.DAY_OF_MONTH, 1);
}
if (negative)
retValue = retValue * -1;
return retValue;
}
} // TimeUtil } // TimeUtil