Search: | |||||

| ||||

Home » Forum » Java | RSS Feeds |

I want to write a program to find the number of working days (Monday

to fir day) between two given dates. Can any one help me how to do it. I

wrote a program for this. But it was now giving correct result for some

condition. It was working for most of the condition but for some

conditions it was not working. I attached the file along with this mail.

The example date given in that program is also showing wrong result as 7

days instead of 2 days. Expecting your comments on it. If there is any

easy logic please let me know.

public int getWorkingDays(int startIndex,int days2go){

int x=days2go;//10 days after

int n=startIndex;//starting week index, from 2nd day of week

int corrector=((7-n)+1); // by this way we are starting to count from

monday

int weeks=((int)((x-corrector)/7));//number of weeks

int full5day=weeks*5;// number of working days in weeks

int firstcorrected=corrector-2;// first remained days, we subtract to

correct index

int remainder=(int)Math.IEEEremainder(x-corrector,7);

//less then 7 using mod 7

// last remained days from full weeks

if(remainder<0){

remainder=x-corrector;

}

int remaineddays;

if(remainder>5){

remaineddays=7-(int)Math.IEEEremainder(remainder,6);

//exclude saturday with mod 6

}else{

remaineddays=remainder;

}

int result=full5day+firstcorrected+remaineddays+1;// ups +1 for correct

your result

return result;

}

Find the sample program that may cater your need. You should be able to enhance

it further. For better understanding, refer Calendar, GregorianCalendar classes.

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

import java.util.Calendar;

import java.util.GregorianCalendar;

public class WorkingDayCalc {

public static void main(String args[]) {

int sYear = 2004,sMonth = 11,sDate = 30;

int eYear = 2005,eMonth = 0,eDate = 3;

Calendar startDate = new GregorianCalendar(sYear,sMonth,sDate);

Calendar endDate = new GregorianCalendar(eYear,eMonth,eDate);

int startDayOfYear = startDate.get(startDate.DAY_OF_YEAR);

int endDayOfYear = endDate.get(endDate.DAY_OF_YEAR);

int totalNumberOfDays = 0;

// Adding one to include both start and end dates.

if(eYear > sYear) {

totalNumberOfDays =

(startDate.getMaximum(startDate.DAY_OF_YEAR)-startDayOfYear)+endDayOfYear + 1;

} else {

totalNumberOfDays = endDayOfYear-startDayOfYear + 1;

}

int numberOfWorkingDays = 0;

for(int i=0;i<totalNumberOfDays;i++) {

if(startDate.get(startDate.DAY_OF_WEEK)!=1 &&

startDate.get(startDate.DAY_OF_WEEK)!=7) {

numberOfWorkingDays++;

}

startDate.add(Calendar.DATE,1);

}

System.out.println("NumberOfWorkingDays : " + numberOfWorkingDays);

System.out.println("TotalNumberOfDays : " + totalNumberOfDays);

}

}

point of view problem

- who select

- what for

For a mobile phone or core banking i'll will select my one.

For a desktop application, i'll select other one.

and also it's not a program, it's a mathematic expression.

Didn't find what you were looking for?
Find more on find the number of working days (monday to firday) between two give
Or get search suggestion and latest updates.

Related Topics:

- type casting /converting seconds to a days hours minutes seconds
- julian day number
- To calculate the time difference between two days
- gcd of given number
- Whether the number is the power of two or not.
- Addition of two numbers
- how to generate a rondom number btween two ranges
- Change every second column to a negative number
- C: Find the longest series of even and positive numbers
- Finding data not in the second list
- Converting a "Stringed" Number to a Number
- Converting a "Stringed" Number to a Number
- exe file(made by visual cafe) does't work (dll file not found error)
- using find find all
- Write a program to read a guest's name, type of room, number of days stayed and mode of payment
- first day of next year
- Highlighting a row on selection of day in calendar
- Need Help - Day Scheduler
- Activesheet.Paste method gives error
- please give me source code
- Can Any one help me writing logic for the given formula
- Create Dynamic Ranges with VB, clue given
- giving the solution of my problem in jtextarea
- convert a given .PHP code into .java class
- Is there anyone to help me for problem given below