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

    Calendar Work Days (only weekend)

    807589
      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 ;
      }
      return totalDays;
      
      }
      
      // 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));
      }
      
      }