Java Day of Week weekDaysBetween(final Date a, final Date b)

Here you can find the source of weekDaysBetween(final Date a, final Date b)

Description

Get week days between two dates inclusive.

License

Open Source License

Declaration

public static int weekDaysBetween(final Date a, final Date b) 

Method Source Code

//package com.java2s;
/**//  w  w  w. j a  v a  2 s .  co m
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

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

public class Main {
    public static final int YEAR = Calendar.YEAR;
    public static final int HOUR = Calendar.HOUR;
    public static final int MINUTE = Calendar.MINUTE;
    public static final int SECOND = Calendar.SECOND;

    /**
     * Get week days between two dates inclusive. (week days between now and
     * now, is 1 if it's week day)
     */
    public static int weekDaysBetween(final Date a, final Date b) {
        int ans = 0;
        final Calendar start = Calendar.getInstance();
        final Calendar end = Calendar.getInstance();
        if (compareDates(a, b) < 0) {
            start.setTime(a);
            end.setTime(b);
        } else {
            start.setTime(b);
            end.setTime(a);
        }
        int wd = start.get(Calendar.DAY_OF_WEEK);
        while ((start.getTime().compareTo(end.getTime()) <= 0) && (wd != Calendar.SUNDAY)) {
            if (wd != Calendar.SATURDAY) {
                ans++;
            }
            start.add(Calendar.DATE, 1);
            wd = start.get(Calendar.DAY_OF_WEEK);
        }

        wd = end.get(Calendar.DAY_OF_WEEK);
        while ((start.getTime().compareTo(end.getTime()) <= 0) && (wd != Calendar.SUNDAY)) {
            if (wd != Calendar.SATURDAY) {
                ans++;
            }
            end.add(Calendar.DATE, -1);
            wd = end.get(Calendar.DAY_OF_WEEK);
        }
        ans += 5 * Math.floor(daysBetween(start.getTime(), end.getTime()) / 7.0);
        return ans;
    }

    /**
     * Compares two dates ignoring time: just year, month, day.
     * 
     * @return < 0 : if dateA < dateB == 0 : if dateA == dateB > 0 : if dateA >
     *         dateB
     */
    public static int compareDates(final Date dateA, final Date dateB) {

        final Calendar calendarA = GregorianCalendar.getInstance();

        calendarA.setTime(dateA);
        calendarA.set(Calendar.MILLISECOND, 0);
        calendarA.set(Calendar.SECOND, 0);
        calendarA.set(Calendar.MINUTE, 0);
        calendarA.set(Calendar.HOUR_OF_DAY, 0);

        final Calendar calendarB = GregorianCalendar.getInstance();

        calendarB.setTime(dateB);
        calendarB.set(Calendar.MILLISECOND, 0);
        calendarB.set(Calendar.SECOND, 0);
        calendarB.set(Calendar.MINUTE, 0);
        calendarB.set(Calendar.HOUR_OF_DAY, 0);

        return calendarA.getTime().compareTo(calendarB.getTime());
    }

    /**
     * Given a Date as parameter, it sets it to the specified time and returns a
     * copy.
     * 
     * @param date
     *            Original date
     * @param hours
     *            Hours to set to the date
     * @param minutes
     *            Minutes to set to the date
     * @param seconds
     *            Seconds to set to the date
     * 
     * @return A new Date with the given time.
     */
    public static Date setTime(final Date date, final int hours, final int minutes, final int seconds) {
        final Calendar aux = Calendar.getInstance();
        aux.setTime(date);
        aux.set(Calendar.MILLISECOND, 0);
        aux.set(Calendar.HOUR, hours);
        aux.set(Calendar.MINUTE, minutes);
        aux.set(Calendar.SECOND, seconds);
        return aux.getTime();
    }

    /**
     * Calculates the number of days that have in common the first interval and
     * the second interval
     */
    public static int daysBetween(final Date dateA, final Date dateB) {

        Date date1, date2;
        date1 = dateA;
        date2 = dateB;

        // Swap dates so that date1 <= date2
        if (date1.after(date2)) {
            date1 = dateB;
            date2 = dateA;
        }
        Calendar calendar1 = GregorianCalendar.getInstance();
        calendar1.setTime(date1);
        final Calendar calendar2 = GregorianCalendar.getInstance();
        calendar2.setTime(date2);

        int days = calendar2.get(java.util.Calendar.DAY_OF_YEAR) - calendar1.get(java.util.Calendar.DAY_OF_YEAR)
                + 1;
        final int y1 = calendar1.get(java.util.Calendar.YEAR);
        final int y2 = calendar2.get(java.util.Calendar.YEAR);
        if (y1 != y2) {
            calendar1 = (Calendar) calendar1.clone();
            do {
                days += calendar1.getActualMaximum(Calendar.DAY_OF_YEAR);
                calendar1.add(java.util.Calendar.YEAR, 1);
            } while (calendar1.get(java.util.Calendar.YEAR) != y2);
        }
        return days - 1;
    }
}

Related

  1. toStartWeek(Date date)
  2. toWeekDayUS(Date date, int expect)
  3. truncDateToIsoWeek(Date d)
  4. weekCount(Date start, Date end)
  5. weekDay(final Date date)
  6. WeekOfTheYear(Date dt)
  7. weekOfYear(Date date)
  8. weeksBetween(Date date1, Date date2)
  9. weeksBetween(Date early, Date late)