Home > Domino Tips > Developer > Java > Java Agent To Determine The Number Of Business Days Between Two Dates
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

JAVA

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


Paul Cathro
05.05.2000
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
Java
Java code inserts data from Notes documents into a SQL table
Java code shortens strings in a SQL table
How to execute a stored procedure in Lotus Notes Domino using Java
How to return an HTML representation of a Lotus Notes rich-text field
Shrink Lotus Notes databases with many attachments
Converting Lotus Notes Domino Web pages to PDF files with a Java agent
A bevy of Notes/Domino development tips
Converting Web pages to images using Java
Creating Microsoft Word documents from Lotus Notes
FAQ: Java for Lotus Notes and Domino

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


);
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);
}

}

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.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts