Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

find the number of working days (monday to firday) between two give

  Asked By: Jesse    Date: Nov 15    Category: Java    Views: 4557

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.



3 Answers Found

Answer #1    Answered By: Ralph Murray     Answered On: Nov 15

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
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

int remaineddays;
//exclude saturday with mod 6

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

Answer #2    Answered By: Keith Marshall     Answered On: Nov 15

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) {

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

Answer #3    Answered By: Timothy Patterson     Answered On: Nov 15

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.