Skip to main content

Getting Businss days in JAVA/ADF

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

Comments

  1. this post was so good .Thanks for uploading this . The provided content was Very informative .
    best sap hcm center|bst sap fiori center

    ReplyDelete
  2. Thanks for Sharing this Valuable Information about Java
    Java Training in Noida

    ReplyDelete
  3. 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.


    Ciitnoida 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

    ReplyDelete
  4. CIIT Noida provides Best MCA Courses in Noida based on the current

    IT 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

    ReplyDelete
  5. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.

    Artificial Intelligence Course
    Java Course
    AWS Course
    Machine Learning Course
    Data Science Course
    DevOps Course

    ReplyDelete

Post a Comment

Popular posts from this blog

Passivation and Activation in ADF (Application Module )

1. For performance reasons, ADF keeps a pool of application modules in memory. It tries to give each session the same application module as the session used during the last request; however, this might not be possible during peak load of your application. 2. In this case, ADF saves the application modules state in a database table so the application module can be used by another session. This is called passivation . 3. When the first session needs the application module again, its state is retrieved from the database process known as activation . 4. If you have made an error in your code and depend on some variable that is not persisted correctly when your application module state is stored, you will experience mysterious errors under high load.   Enable/Disable Application Module Pooling : Right-click on your application module, choose Configurations.By default, each application module has two configurations. Ensure that the one ending in …Local is selected and then c...

Get modified rows from Entitiy Cache

To get the modified rows from entity cache we have getEntityState() method at EntityImpl class. Refer to my previous blog  Accessing EO impl methods from VO impl  where i am overriding the getEntityState() in EOimpl and calling it in VOImpl. We can use methods written or overridden in VOImpl class to AMImpl class. There are different states associated with an entity object. STATUS_UNMODIFIED STATUS_MODIFIED STATUS_NEW STATUS_DELETED STATUS_DEAD We have to check the state or row in our AmImpl class by using the VOImpl method and through this we can distinguish the rows present at vo. Add below code in AMImpl class along with my previous post. public void geCachedRowsCount(){         JobsVOImpl jobsVo = (JobsVOImpl)this.getJobsVO();         RowSetIterator iter = jobsVo.createRowSetIterator(null);             while(iter.hasNext()){             Row row = iter....

The file store "WLS_DIAGNOSTICS" could not be opened

WLS_DIAGNOSTIC ERROR weblogic.store.PersistentStoreException: [Store:280073]The file store "WLS_DIAGNOSTICS" could not be opened because it contained a file with the invalid version 1. A file of version 2 was expected. When you get this error while running your application on internal weblogic server delete the following file WLS_DIAGNOSTICS000000.DAT search the file in following path C:\jdev_work\system11.1.1.5.37.60.13\DefaultDomain this file is in DefaultDomain folder of your jdev. and delete the WLS_DIAGNOSTICS000000.DAT file . and run your applicatuon