Follow us on
You are here: Home > All Posts > N/A > hsbc > technical

Java code to exclude weekend and holidays
Posted by Santosh ( Verified Professional) from hsbc                                              
Posted On 1 year ago | Last Modified 294 Day 5 hour ago | Visit1790
City: N/A

mport java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class WorkingDaysFinder {

    static List<Date>  holidayList =  new ArrayList<Date>();
   
    // holiday list
    static{
        holidayList.add(new Date("08/18/2009"));
        holidayList.add(new Date("08/19/2009"));
    }
   
    public static void main(String args[]){
       
        Date day1 = new Date("08/17/2009"); // start date
        Date day2 = new Date("08/27/2009"); // end date
        Date dayCounter = day1;
        int counter = 1;
       
        while(dayCounter.before(day2)){
       
            // check for weekends and holiday list
            if(dayCounter.getDay() != Calendar.SATURDAY &&
               dayCounter.getDay()!=Calendar.SUNDAY &&
               !holidayList.contains(dayCounter))
            {
                counter++; // count weekdays
            }
           
            dayCounter.setDate(dayCounter.getDate()+1);
        }
       
        System.out.println("Working days = "+counter);
       
    }
}

Comments
@ganesh, this is just an example... i know software engineers do copy and paste stuff but u can do little bit with your self :-)
- miraj 294 Day 5 hour ago
I guess we should avoid System.out.println...........Isn't it ??
- ganesh 294 Day 6 hour ago
wow!! it is totally worth for me... thx @Santosh
- ravi 300 Day 9 hour ago
@Nirav - I think it is already generic, what else do you else expect...Wanted to know just for my understanding..no offense
- santosh 300 Day 9 hour ago
Code should be generic
- Nirav 303 Day 13 hour ago
ohh.. Its so easy, why couldn't I think that... Thanks it helped.
- gopesh 304 Day 7 hour ago




Related Posts View All
Tags :