
JAVA
Java Agent To Determine The Number Of Business Days Between Two Dates
Paul Cathro 05.05.2000
Rating: --- (out of 5)




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).
Code
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 = " + 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); } }
 |

|
Rate this Tip
|
To rate tips, you must be a member of SearchDomino.com. Register now
to start rating these tips. Log in if you are already a member.
|


');
// -->
DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.
|
 |
|
|
 |
|
 |