Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. /** Returns number of Saturday and Sunday between 2 dates */
  2. public static int getWeekendsDaysBetween(Timestamp startDate, Timestamp endDate, int clientID, String trxName)
  3. {
  4. int retValue = 0;
  5.  
  6. if (startDate.equals(endDate))
  7. return 0;
  8.  
  9. boolean negative = false;
  10. if (endDate.before(startDate)) {
  11. negative = true;
  12. Timestamp temp = startDate;
  13. startDate = endDate;
  14. endDate = temp;
  15. }
  16.  
  17. ArrayList<Timestamp> nbd = new ArrayList<Timestamp>();
  18. StringBuilder sql = new StringBuilder("SELECT Date1, Name FROM C_NonBusinessDay WHERE AD_Client_ID IN (0, ").append(clientID)
  19. .append(") AND Date1 >= ").append(DB.TO_DATE(startDate)).append(" AND Date1 <= ").append(DB.TO_DATE(endDate));
  20. PreparedStatement ps = null;
  21. ResultSet rs = null;
  22. try {
  23. ps = DB.prepareStatement(sql.toString(), trxName);
  24. rs = ps.executeQuery();
  25. while (rs.next())
  26. nbd.add(rs.getTimestamp(1));
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. } finally {
  30. DB.close(rs, ps);
  31. }
  32.  
  33. GregorianCalendar cal = new GregorianCalendar();
  34. cal.setTime(startDate);
  35. cal.set(Calendar.HOUR_OF_DAY, 0);
  36. cal.set(Calendar.MINUTE, 0);
  37. cal.set(Calendar.SECOND, 0);
  38. cal.set(Calendar.MILLISECOND, 0);
  39.  
  40. GregorianCalendar calEnd = new GregorianCalendar();
  41. calEnd.setTime(endDate);
  42. calEnd.set(Calendar.HOUR_OF_DAY, 0);
  43. calEnd.set(Calendar.MINUTE, 0);
  44. calEnd.set(Calendar.SECOND, 0);
  45. calEnd.set(Calendar.MILLISECOND, 0);
  46.  
  47. while (cal.before(calEnd) || cal.equals(calEnd)) {
  48. if (!nbd.contains(new Timestamp(cal.getTimeInMillis()))) {
  49. if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
  50. retValue++;
  51. }
  52. cal.add(Calendar.DAY_OF_MONTH, 1);
  53. }
  54.  
  55. retValue--;
  56.  
  57. if (negative)
  58. retValue = retValue * -1;
  59. return retValue;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement