Manage Learn to apply best practices and optimize your operations.

Java Agent To Determine The Number Of Business Days Between Two Dates

I needed to determine the number of business days between two dates and had the
formula version but wanted to learn Java agents so I created this
agent that given two dates will return the number of business days (excluding
weekend days).
import lotus.domino.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class CalcBusinessDays extends AgentBase {

public void NotesMain() {
DateTime startTime = null, endTime = null;
String startTimeStr, endTimeStr, result;

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();

startTimeStr = "04/12/2000";
endTimeStr = "05/04/2000";
startTime = session.createDateTime(startTimeStr);
endTime = session.createDateTime(endTimeStr);
result = diffInWeekdays(startTime, endTime, startTimeStr,
endTimeStr);
System.out.println("Result = " + result);

} catch(Exception e) {
e.printStackTrace();
}
}

public String diffInWeekdays(DateTime startTime, DateTime endTime, String
startTimeStr, String endTimeStr) {
String res = "";
try {
Date firstDate = null, secondDate = null;
int diffInt = endTime.timeDifference(startTime);
int diffIntDays = (diffInt / 86400 + 1);
BigInteger sev = BigInteger.valueOf(7);
BigInteger minusTwo = BigInteger.valueOf(-2);
BigInteger bis = BigInteger.valueOf(getWeekday(firstDate =
dateConvert(startTimeStr)));
BigInteger bie = BigInteger.valueOf(getWeekday(secondDate =
dateConvert(endTimeStr)));
int strtDay = bis.mod(sev).intValue();
int endDay = bie.mod(sev).intValue();
int max = minusTwo.max(BigInteger.valueOf(strtDay * -1)).intValue();
int min = BigInteger.valueOf(1).min(bie.mod(sev)).intValue();
int result = (diffIntDays - endDay + strtDay - 8) * 5 / 7 - max - min
+ 5 - strtDay + endDay;
//o.println("result =\t" + result);
res = Integer.toString(result);
} catch (Exception e) {
e.printStackTrace();
}
return res;
}

public Date dateConvert(String dt) {
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
ParsePosition pos = new ParsePosition(0);
Date covDate = sdf.parse(dt, pos);
return covDate;
}

public int getWeekday(Date cdt) {
Calendar cal = Calendar.getInstance();
cal.setTime(cdt);
return cal.get(Calendar.DAY_OF_WEEK);
}

}

Dig Deeper on Domino Resources - Part 7

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close