Use the following class in your application to get the Business days
getBusinessDateAdded - is the method which will accept the number of daya of future ,and second parameter is the DB connection as here i am using the DB query direct to get the holidays.
you can customise this as per your need
package org.adr.prism.casemgt.ui.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.time.DateUtils;
/* This bean is used to get the business days on the given oracle domain date
* 04-Julu-2013
* @sachin
*/
public class BusinessDays {
public BusinessDays() {
super();
}
private static transient Map<Integer, List<Date>> computedDates = new HashMap<Integer, List<Date>>();
public oracle.jbo.domain.Date getBusinessDateAdded(oracle.jbo.domain.Date createdDate,Connection con,int no ) {
Date date=createdDate.dateValue();
for(int i=0;i<no;i++){
date=getNextBusinessDay(date);
System.out.println(date);
date=checkHoliday(date,con);
}
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(sqlDate);
System.out.println(jboDate);
return jboDate;
}
public Date checkHoliday(Date date, Connection con) {
PreparedStatement prep = null;
ResultSet rs = null;
Date validDate = null;
SimpleDateFormat dt1 = new SimpleDateFormat("dd-MMM-yy");
System.out.println(dt1.format(date));
try {
String query ="select holiday_date from holiday_calender where holiday_date='" +dt1.format(date) + "'";
System.out.println("query - >"+query);
prep = con.prepareStatement(query);
rs = prep.executeQuery();
if (rs.next()) {
validDate = getNextBusinessDay(date);
return validDate;
} else
return date;
} catch (SQLException e) {
}
return date;
}
public Date getNextBusinessDay(Date startDate) {
System.out.println("Date in getNextBusinessDay - >"+startDate);
//Decrement the Date object by a Day and clear out hour/min/sec information
Date nextDay =DateUtils.truncate(addDays(startDate, 1), Calendar.DATE);
//If yesterday is a valid business day, return it
if (isBusinessDay(nextDay))
return nextDay;
//Else we recursively call our function until we find one.
else
return getNextBusinessDay(nextDay);
}
public boolean isBusinessDay(Date dateToCheck) {
System.out.println("Date in isBusinessDay - >"+dateToCheck);
//Setup the calendar to have the start date truncated
Calendar baseCal = Calendar.getInstance();
baseCal.setTime(DateUtils.truncate(dateToCheck, Calendar.DATE));
List<Date> offlimitDates;
//Grab the list of dates for the year. These SHOULD NOT be modified.
synchronized (computedDates) {
int year = baseCal.get(Calendar.YEAR);
//If the map doesn't already have the dates computed, create them.
if (!computedDates.containsKey(year))
computedDates.put(year, getOfflimitDates(year));
offlimitDates = computedDates.get(year);
}
//Determine if the date is on a weekend.
int dayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK);
boolean onWeekend =dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY;
//If it's on a holiday, increment and test again
//If it's on a weekend, increment necessary amount and test again
//System.out.println(offlimitDates);
if (offlimitDates.contains(baseCal.getTime()) || onWeekend)
return false;
else
return true;
}
/**
* Based on a year, this will compute the actual dates of
*
*
**/
public List<Date> getOfflimitDates(int year) {
System.out.println("Year in getOfflimitDates - >"+year);
List<Date> offlimitDates = new ArrayList<Date>();
Calendar baseCalendar = GregorianCalendar.getInstance();
baseCalendar.clear();
//Add in the static dates for the year.
//New years day
baseCalendar.set(year, Calendar.JANUARY, 1);
offlimitDates.add(baseCalendar.getTime());
//Christmas
baseCalendar.set(year, Calendar.DECEMBER, 25);
offlimitDates.add(baseCalendar.getTime());
baseCalendar.set(year, Calendar.DECEMBER, 26);
offlimitDates.add(baseCalendar.getTime());
// //Greece Specific
// Date pasxa = getOrthodoxEaster(year);
// offlimitDates.add(pasxa);
// offlimitDates.add(addDays(pasxa, -3));
// offlimitDates.add(addDays(pasxa, 1));
// //25 March
// baseCalendar.set(year, Calendar.MARCH, 25);
// offlimitDates.add(baseCalendar.getTime());
// //28 October
// baseCalendar.set(year, Calendar.OCTOBER, 28);
// offlimitDates.add(baseCalendar.getTime());
// //Fwta
// baseCalendar.set(year, Calendar.JANUARY, 6);
// offlimitDates.add(baseCalendar.getTime());
//May Day
// baseCalendar.set(year, Calendar.MAY, 1);
// offlimitDates.add(baseCalendar.getTime());
// // 15 August
// baseCalendar.set(year, Calendar.AUGUST, 15);
// offlimitDates.add(baseCalendar.getTime());
return offlimitDates;
}
/**
* Compute the day of the year that Orthodox Easter falls on.
* Based on Gausean Algorithm.
*
* @param the_year Year
* @return Orthodox Easter Sunday
*/
public Date getOrthodoxEaster(int the_year) {
//Gaus Algorithm
Date easter;
int res =(19 * (the_year % 19) + 16) % 30 + (2 * (the_year % 4) + 4 * (the_year %7) +6 *(((19 * (the_year % 19)) + 16) %30)) % 7 + 3;
if (res < 31)
easter =(new GregorianCalendar(the_year, Calendar.APRIL, res)).getTime(); // 4-1 // pasxa = res+"/"+"04"+"/"+the_year;
else
easter =(new GregorianCalendar(the_year, Calendar.MAY, res - 30)).getTime(); // 5-1 // pasxa = (res-30)+"/"+"05"+"/"+the_year;
return easter;
}
/**
* Private method simply adds
* @param dateToAdd
* @param numberOfDay
* @return
*/
public Date addDays(Date dateToAdd, int numberOfDay) {
if (dateToAdd == null)
throw new IllegalArgumentException("Date can't be null!");
Calendar tempCal = Calendar.getInstance();
tempCal.setTime(dateToAdd);
tempCal.add(Calendar.DATE, numberOfDay);
return tempCal.getTime();
}
}
getBusinessDateAdded - is the method which will accept the number of daya of future ,and second parameter is the DB connection as here i am using the DB query direct to get the holidays.
you can customise this as per your need
package org.adr.prism.casemgt.ui.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.time.DateUtils;
/* This bean is used to get the business days on the given oracle domain date
* 04-Julu-2013
* @sachin
*/
public class BusinessDays {
public BusinessDays() {
super();
}
private static transient Map<Integer, List<Date>> computedDates = new HashMap<Integer, List<Date>>();
public oracle.jbo.domain.Date getBusinessDateAdded(oracle.jbo.domain.Date createdDate,Connection con,int no ) {
Date date=createdDate.dateValue();
for(int i=0;i<no;i++){
date=getNextBusinessDay(date);
System.out.println(date);
date=checkHoliday(date,con);
}
java.sql.Date sqlDate = new java.sql.Date(date.getTime());
oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(sqlDate);
System.out.println(jboDate);
return jboDate;
}
public Date checkHoliday(Date date, Connection con) {
PreparedStatement prep = null;
ResultSet rs = null;
Date validDate = null;
SimpleDateFormat dt1 = new SimpleDateFormat("dd-MMM-yy");
System.out.println(dt1.format(date));
try {
String query ="select holiday_date from holiday_calender where holiday_date='" +dt1.format(date) + "'";
System.out.println("query - >"+query);
prep = con.prepareStatement(query);
rs = prep.executeQuery();
if (rs.next()) {
validDate = getNextBusinessDay(date);
return validDate;
} else
return date;
} catch (SQLException e) {
}
return date;
}
public Date getNextBusinessDay(Date startDate) {
System.out.println("Date in getNextBusinessDay - >"+startDate);
//Decrement the Date object by a Day and clear out hour/min/sec information
Date nextDay =DateUtils.truncate(addDays(startDate, 1), Calendar.DATE);
//If yesterday is a valid business day, return it
if (isBusinessDay(nextDay))
return nextDay;
//Else we recursively call our function until we find one.
else
return getNextBusinessDay(nextDay);
}
public boolean isBusinessDay(Date dateToCheck) {
System.out.println("Date in isBusinessDay - >"+dateToCheck);
//Setup the calendar to have the start date truncated
Calendar baseCal = Calendar.getInstance();
baseCal.setTime(DateUtils.truncate(dateToCheck, Calendar.DATE));
List<Date> offlimitDates;
//Grab the list of dates for the year. These SHOULD NOT be modified.
synchronized (computedDates) {
int year = baseCal.get(Calendar.YEAR);
//If the map doesn't already have the dates computed, create them.
if (!computedDates.containsKey(year))
computedDates.put(year, getOfflimitDates(year));
offlimitDates = computedDates.get(year);
}
//Determine if the date is on a weekend.
int dayOfWeek = baseCal.get(Calendar.DAY_OF_WEEK);
boolean onWeekend =dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY;
//If it's on a holiday, increment and test again
//If it's on a weekend, increment necessary amount and test again
//System.out.println(offlimitDates);
if (offlimitDates.contains(baseCal.getTime()) || onWeekend)
return false;
else
return true;
}
/**
* Based on a year, this will compute the actual dates of
*
*
**/
public List<Date> getOfflimitDates(int year) {
System.out.println("Year in getOfflimitDates - >"+year);
List<Date> offlimitDates = new ArrayList<Date>();
Calendar baseCalendar = GregorianCalendar.getInstance();
baseCalendar.clear();
//Add in the static dates for the year.
//New years day
baseCalendar.set(year, Calendar.JANUARY, 1);
offlimitDates.add(baseCalendar.getTime());
//Christmas
baseCalendar.set(year, Calendar.DECEMBER, 25);
offlimitDates.add(baseCalendar.getTime());
baseCalendar.set(year, Calendar.DECEMBER, 26);
offlimitDates.add(baseCalendar.getTime());
// //Greece Specific
// Date pasxa = getOrthodoxEaster(year);
// offlimitDates.add(pasxa);
// offlimitDates.add(addDays(pasxa, -3));
// offlimitDates.add(addDays(pasxa, 1));
// //25 March
// baseCalendar.set(year, Calendar.MARCH, 25);
// offlimitDates.add(baseCalendar.getTime());
// //28 October
// baseCalendar.set(year, Calendar.OCTOBER, 28);
// offlimitDates.add(baseCalendar.getTime());
// //Fwta
// baseCalendar.set(year, Calendar.JANUARY, 6);
// offlimitDates.add(baseCalendar.getTime());
//May Day
// baseCalendar.set(year, Calendar.MAY, 1);
// offlimitDates.add(baseCalendar.getTime());
// // 15 August
// baseCalendar.set(year, Calendar.AUGUST, 15);
// offlimitDates.add(baseCalendar.getTime());
return offlimitDates;
}
/**
* Compute the day of the year that Orthodox Easter falls on.
* Based on Gausean Algorithm.
*
* @param the_year Year
* @return Orthodox Easter Sunday
*/
public Date getOrthodoxEaster(int the_year) {
//Gaus Algorithm
Date easter;
int res =(19 * (the_year % 19) + 16) % 30 + (2 * (the_year % 4) + 4 * (the_year %7) +6 *(((19 * (the_year % 19)) + 16) %30)) % 7 + 3;
if (res < 31)
easter =(new GregorianCalendar(the_year, Calendar.APRIL, res)).getTime(); // 4-1 // pasxa = res+"/"+"04"+"/"+the_year;
else
easter =(new GregorianCalendar(the_year, Calendar.MAY, res - 30)).getTime(); // 5-1 // pasxa = (res-30)+"/"+"05"+"/"+the_year;
return easter;
}
/**
* Private method simply adds
* @param dateToAdd
* @param numberOfDay
* @return
*/
public Date addDays(Date dateToAdd, int numberOfDay) {
if (dateToAdd == null)
throw new IllegalArgumentException("Date can't be null!");
Calendar tempCal = Calendar.getInstance();
tempCal.setTime(dateToAdd);
tempCal.add(Calendar.DATE, numberOfDay);
return tempCal.getTime();
}
}
Excellent post define very nicely point.Thanks for the sharing.
ReplyDeleteWeb Designing training in noida | SAS Summer Training in Noida
this post was so good .Thanks for uploading this . The provided content was Very informative .
ReplyDeletebest sap hcm center|bst sap fiori center
Thanks for Sharing this Valuable Information about Java
ReplyDeleteJava Training in Noida
CIITN is the Best Php training institute in Noida and delhi Ncr. You will get Live Project Training on PHP by our PHP expert who have 5+ year industrial experience.Focus on practical and live project training. In our PHP training, we you will learn core PHP, advance PHP, HTML, CSS, JavaScript, jQuery, Bootstrap, Cake PHP and Wordpress.CIITN provides 100% job assistance in PHP training. CIITN is well known PHP coaching center because our 100% PHP students are placed now.
ReplyDeleteCiitnoida provides Core and java training institute in noida. We have a team of experienced Java professionals who help our students learn Java with the help of Live Base Projects. The object-oriented, class-based build of Java has made it one of most popular programming languages and the demand of professionals with certification in Advance Java training is at an all-time high not just in India but foreign countries too.
By helping our students understand the fundamentals and Advance concepts of Java, we prepare them for a successful programming career. With over 13 years of sound experience, we have successfully trained hundreds of students in Noida and have been able to turn ourselves into an institute for best Java training in Noida.
java training institute in noida
php training in noida
linux training in noida
linux institute in noida
java course in noida
CIIT Noida provides Best MCA Courses in Noida based on the current
ReplyDeleteIT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad.
CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced
level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.
At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live
projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to
students that transform an average student into a corporate professional whom companies prefer to hire.
Best MCA College in Noida
Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
ReplyDeleteArtificial Intelligence Course
Java Course
AWS Course
Machine Learning Course
Data Science Course
DevOps Course