Java Day Between getWorkingDaysBetween(Date fromDate, Date toDate)

Here you can find the source of getWorkingDaysBetween(Date fromDate, Date toDate)

Description

get Working Days Between

License

Open Source License

Declaration

public static int getWorkingDaysBetween(Date fromDate, Date toDate) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Boeing./*  w  w w . j a v a  2 s.  co  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static int getWorkingDaysBetween(Date fromDate, Date toDate) {
        return getWorkingDaysBetween(getCalendar(fromDate), getCalendar(toDate));
    }

    public static int getWorkingDaysBetween(Calendar fromDate, Calendar toDate) {
        int workingDays = 0;
        while (!fromDate.after(toDate)) {
            int day = fromDate.get(Calendar.DAY_OF_WEEK);
            if (day != Calendar.SATURDAY && day != Calendar.SUNDAY) {
                workingDays++;
            }

            fromDate.add(Calendar.DATE, 1);
        }
        return workingDays;
    }

    public static Calendar getCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public static String get(Date date) {
        if (date == null) {
            return "";
        }
        return DateFormat.getDateInstance().format(date);
    }

    public static String get(Date date, String pattern) {
        return get(date, new SimpleDateFormat(pattern));
    }

    public static String get(Date date, DateFormat dateFormat) {
        if (date == null) {
            return "";
        }
        String result = dateFormat.format(date);
        return result;
    }
}

Related

  1. getNumDaysDiffExclTime(Date date1, Date date2)
  2. getTimeDifference(Date d1, Date d2)
  3. getTimeDifference(Date date1, Date date2)
  4. getTimeDifference(Date otherDate)
  5. getTodayDiff(String diffDate)
  6. getYearDiff(Date date1, Date date2)
  7. getYearsDifference(Date startTime, Date endTime)
  8. hourDiff(Date firstDate, Date lastDate)
  9. internalTrimOrAlterDate(Date date, boolean trim, int dayDiff)