Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Returns number of Saturday and Sunday between 2 dates */
- public static int getWeekendsDaysBetween(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;
- }
- ArrayList<Timestamp> nbd = new ArrayList<Timestamp>();
- StringBuilder sql = new StringBuilder("SELECT Date1, Name FROM C_NonBusinessDay WHERE AD_Client_ID IN (0, ").append(clientID)
- .append(") AND Date1 >= ").append(DB.TO_DATE(startDate)).append(" AND Date1 <= ").append(DB.TO_DATE(endDate));
- PreparedStatement ps = null;
- ResultSet rs = null;
- try {
- ps = DB.prepareStatement(sql.toString(), trxName);
- rs = ps.executeQuery();
- while (rs.next())
- nbd.add(rs.getTimestamp(1));
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- DB.close(rs, ps);
- }
- 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);
- }
- retValue--;
- if (negative)
- retValue = retValue * -1;
- return retValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement