vibhav
asked on
computing week days between 2 dates
Can i get a code snippet for calculating / computing only week days (excluding saturday and sundays) between two dates (from and to date).
Thanks,
Thanks,
ASKER
can i get a complete code please...
That will only work on days in the same year...
If, when the end date is on a weekend, you don't want to count a day for it:
private long getWeekdayDifference(Date startDate, Date endDate) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endD ate, startDate);
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);
long days = 0;
while (!cal.getTime().equals(end Date)) {
int weekDay = cal.get(Calendar.DAY_OF_WE EK);
cal.add(Calendar.DAY_OF_WE EK,1);
if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
days++;
}
return days;
}
If, when the end date is on a weekend, you DO want to count 1 day (as if it were the following monday):
private long getWeekdayDifference(Date startDate, Date endDate) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endD ate, startDate);
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);
long days = 0;
while (!cal.getTime().equals(end Date)) {
int weekDay = cal.get(Calendar.DAY_OF_WE EK);
if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
days++;
cal.add(Calendar.DAY_OF_WE EK,1);
}
return days;
}
private long getWeekdayDifference(Date startDate, Date endDate) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endD
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);
long days = 0;
while (!cal.getTime().equals(end
int weekDay = cal.get(Calendar.DAY_OF_WE
cal.add(Calendar.DAY_OF_WE
if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
days++;
}
return days;
}
If, when the end date is on a weekend, you DO want to count 1 day (as if it were the following monday):
private long getWeekdayDifference(Date startDate, Date endDate) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endD
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);
long days = 0;
while (!cal.getTime().equals(end
int weekDay = cal.get(Calendar.DAY_OF_WE
if (weekDay != Calendar.SATURDAY && weekDay != Calendar.SUNDAY)
days++;
cal.add(Calendar.DAY_OF_WE
}
return days;
}
that is, from a friday to the following saturday, if you want to count that as 1, use the second one, otherwise use the first.
ASKER
Thanks dberner9.
Your code works fine for me...
I need one more favour.. I need to find end date from given from date which contains 100 working days.
Ex: If start date given is 05/01/2005, end date should be calculated as 09/19/2005.
Can i also get a code snippet for this.
Your code works fine for me...
I need one more favour.. I need to find end date from given from date which contains 100 working days.
Ex: If start date given is 05/01/2005, end date should be calculated as 09/19/2005.
Can i also get a code snippet for this.
private long getEndDate(Date startDate, long businessDays) {
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endD ate, startDate);
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);
long days = 0;
for (long i = 0; i < businessDays; i++) {
int weekDay = cal.get(Calendar.DAY_OF_WE EK);
if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) i--;
cal.add(Calendar.DAY_OF_WE EK,1);
}
return cal.getTime();
}
if (startDate.getTime() > endDate.getTime()) return -getWeekdayDifference(endD
Calendar cal = new GregorianCalendar();
cal.setTime(startDate);
long days = 0;
for (long i = 0; i < businessDays; i++) {
int weekDay = cal.get(Calendar.DAY_OF_WE
if (weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY) i--;
cal.add(Calendar.DAY_OF_WE
}
return cal.getTime();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Calendar cal = Calendar.getInstance().set
You can get week number of year:
cal.get(Calendar.WEEK_OF_Y
day of week:
cal.get(Calendar.DAY_OF_WE
and so on.
Having this, You can find full weeks passed, and find the days:
(fullweekcount - 1) * 5 + first week day count + last week day count