10 Replies Latest reply: Nov 13, 2008 12:23 AM by 807589

# Calendar Work Days (only weekend)

Hi,
I have this code and my goal is to calculate the all days includs weekends by getting number of work days.
Weekend = FRIDAY + SATURDAY
For example: if I get 7 days --> I want return 9 days
``````import java.util.Calendar;

public class CalendarWorkDays {

public int getNumberOfWorkDays(int days) {
java.util.Calendar rightNowCalendar = Calendar.getInstance();
int numberOfDaysWorksForThisWeek = getNumberOfDaysWorksForThisWeek(rightNowCalendar
.get(Calendar.DAY_OF_WEEK));
int workDays = days - numberOfDaysWorksForThisWeek;
int totalDays;
if (workDays <= 5)
totalDays = days + 2;

else {
int weekendDays = (workDays / 7) * 2;
totalDays = (workDays - weekendDays) + workDays%7 ;
}

}

// get number of days from today until the weekend (Friday) include today.
public int getNumberOfDaysWorksForThisWeek(int dayOfWeek) {

switch (dayOfWeek) {
case Calendar.SUNDAY:
return 5;
case Calendar.MONDAY:
return 4;
case Calendar.TUESDAY:
return 3;
case Calendar.WEDNESDAY:
return 2;
case Calendar.THURSDAY:
return 1;
case Calendar.FRIDAY:
return 0;
case Calendar.SATURDAY:
return 0;
default:
throw new Error("Unknown dayOfWeek: " + dayOfWeek);
}
}
Thanks alot and have a nice day,
Yael.

public static void main(String[] args) {
CalendarWorkDays calendarWorkDays = new CalendarWorkDays();
System.out.println(calendarWorkDays.getNumberOfWorkDays(22));
}

}``````
• ###### 2. Re: Calendar Work Days (only weekend)
I have this method and it's working:
``````import java.util.Calendar;

public class CalendarWorkDays {

public static int calculateNumberOfDays(int numberOfWorkingDays){

java.util.Calendar rightNowCalendar = Calendar.getInstance();
int dayOfWeek = rightNowCalendar
.get(Calendar.DAY_OF_WEEK);

int numOfFullWeeks= numberOfWorkingDays/5;

int numOfRemainingDays=numberOfWorkingDays-(numOfFullWeeks*5);

int numberOfDays=numOfFullWeeks*7+numOfRemainingDays;

if(dayOfWeek+numOfRemainingDays>6){

numberOfDays=numberOfDays+2;
}
return numberOfDays;
}

public static void main(String[] args) {

System.out.println(calculateNumberOfDays(9));
}

}``````
Do you know how to make it more a fishint?
Thank you alot
• ###### 3. Re: Calendar Work Days (only weekend)
yael800 wrote:
make it more a fishint
A what?
• ###### 4. Re: Calendar Work Days (only weekend)
BalusC wrote:
yael800 wrote:
make it more a fishint
A what?
I'm guessing s/he meant "efficient"; try reading it aloud.

~
• ###### 5. Re: Calendar Work Days (only weekend)
yael800 wrote:
Do you know how to make it more a fishint?
What kind of a fishincy were you interested in? I can't imagine it takes a long time to run that code. And it doesn't appear to use much memory at all. And the code is pretty readable. So what more do you need?
• ###### 6. Re: Calendar Work Days (only weekend)
DrClap wrote:
What kind of a fishincy were you interested in?
Wouldn't that be "a fish in sea"?

~
• ###### 7. Re: Calendar Work Days (only weekend)
yawmark wrote:
DrClap wrote:
What kind of a fishincy were you interested in?
Wouldn't that be "a fish in sea"?

~
I lol'd.
• ###### 8. Re: Calendar Work Days (only weekend)
LOL!