Java Day of Week getWeekDaysBetween(Date fromDate, Date toDate, int dayOfWeek)

Here you can find the source of getWeekDaysBetween(Date fromDate, Date toDate, int dayOfWeek)

Description

get Week Days Between

License

Open Source License

Declaration

public static int getWeekDaysBetween(Date fromDate, Date toDate, int dayOfWeek) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    private static final int DAYS_IN_A_WEEK = 7;

    public static int getWeekDaysBetween(Date fromDate, Date toDate, int dayOfWeek) {
        int result = 0;
        Date firstDate = getFirstWeekdayBetween(fromDate, toDate, dayOfWeek);
        if (firstDate == null) {
            return 0;
        }//from www  .  ja v  a2s .c o m
        Calendar aDay = Calendar.getInstance();
        aDay.setTime(firstDate);
        while (aDay.getTime().before(toDate)) {
            result++;
            aDay.add(Calendar.DATE, DAYS_IN_A_WEEK);
        }
        return result;
    }

    public static Date getFirstWeekdayBetween(Date fromDate, Date toDate, int dayOfWeek) {
        Calendar aDay = Calendar.getInstance();
        aDay.setTime(fromDate);
        while (aDay.getTime().before(toDate)) {
            if (aDay.get(Calendar.DAY_OF_WEEK) == dayOfWeek) {
                return aDay.getTime();
            }
            aDay.add(Calendar.DATE, 1);
        }
        return null;
    }
}

Related

  1. getWeekDay(String strDate)
  2. getWeekdayDate(Date actualDate)
  3. getWeekdayInterval(Date date)
  4. getWeekDayOfYear(Date date)
  5. getWeekDays(String _date, int _amount)
  6. getWeekEnd(Date date, int w)
  7. getWeekFirstDay(Date weekday)
  8. getWeekID(Date dateValue)
  9. getWeekNum(Date date)