Skip to main content

Get Business Days in JAVA

you can customise this code as per your requirement
if u need previous business day then just give - value instead of 1 in red area

---------------------------------------------import files-------------------------------------

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log.Logger;


   ----------------------------------------


 private void setExpectedPerfectionDate() {
        Row printakeRow =ADFUtils.findIterator("CasePreIntakeVOIterator").getCurrentRow();
        oracle.jbo.domain.Date createdDate=null,updatedDate=null;
        if (printakeRow != null) {
            createdDate = (oracle.jbo.domain.Date)printakeRow.getAttribute("CreatedDttm");
            updatedDate=getTwoBusinessDateAdded(createdDate);
            ViewObject casesVo = ADFUtils.findIterator("CasesVOIterator").getViewObject();
//             casesVo.setNamedWhereClauseParam("bindCaseId",printakeRow.getAttribute("CaseId").toString());
//             casesVo.executeQuery();
            Row row = casesVo.getCurrentRow();
            if(row!=null){
                row.setAttribute("ExpectedPerfectionDate", updatedDate);
                ADFUtils.findOperation("Commit").execute();
            }
           
           
         }
       
    }

--------------this method wil add two business days ----------------------

    private oracle.jbo.domain.Date getTwoBusinessDateAdded(oracle.jbo.domain.Date createdDate) {
        oracle.jbo.domain.Date date=new oracle.jbo.domain.Date();
        Date Date=getNextBusinessDay(getNextBusinessDay(createdDate.dateValue()));
        java.sql.Date sqlDate = new java.sql.Date(Date.getTime());
        oracle.jbo.domain.Date jboDate = new oracle.jbo.domain.Date(sqlDate);
        return jboDate;
    }
   
--------------------------- get the next business day------------

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

-----------------------------it returns wheather the nest day is business day or not-----------

    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
     *
     *
     **/
    private  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
     */
    private  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

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