Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use:</p> <pre><code>public Calendar addBusinessDate(Calendar cal, int numBusinessDays) { int numNonBusinessDays = 0; for(int i = 0; i &lt; numBusinessDays; i++) { cal.add(Calendar.DATE, 1); /* It's a Canadian/American custom to get the Monday (sometimes Friday) off when a holiday falls on a weekend. */ for(int j = 0; j &lt; holidays; j++) { //holidays is list of dates if(cal.getTime() == (Date)holidays.get(j)) { numNonBusinessDays++; } } if(cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7) { numNonBusinessDays++; } } if(numNonBusinessDays &gt; 0) { cal.add(Calendar.DATE, numNonBusinessDays); } return cal; } </code></pre> <p>You'd have to populate a list of dates in order to handle holidays. There's common ones like New Years, but Thanksgiving is different between Canada &amp; the US for instance. Also mind that holidays can fall on a weekend, so the weekend becomes a 3 day weekend.</p> <p>Reference:</p> <ul> <li><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html" rel="nofollow noreferrer">Calendar</a></li> <li><a href="http://java.sun.com/j2se/1.4.2/docs/api/constant-values.html#java.util.Calendar.DAY_OF_WEEK" rel="nofollow noreferrer">Calendar Constant Values</a></li> </ul> <p>PS: There isn't really a need to return the Calendar instance if you are updating the value as in the example. But it is valid if you want to create a separate Calendar instance, use:</p> <pre><code>public Calendar addBusinessDate(Calendar cal, int numBusinessDays) { Calendar cal2 = Calendar.getInstance(); cal2.setTime(cal.getTime()); int numNonBusinessDays = 0; for(int i = 0; i &lt; numBusinessDays; i++) { cal2.add(Calendar.DATE, 1); /* It's a Canadian/American custom to get the Monday (sometimes Friday) off when a holiday falls on a weekend. */ for(int j = 0; j &lt; holidays; j++) { //holidays is list of dates if(cal2.getTime() == (Date)holidays.get(j)) { numNonBusinessDays++; } } if(cal2.get(Calendar.DAY_OF_WEEK) == 1 || cal2.get(Calendar.DAY_OF_WEEK) == 7) { numNonBusinessDays++; } } if(numNonBusinessDays &gt; 0) { cal2.add(Calendar.DATE, numNonBusinessDays); } return cal2; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload