Java Date Difference diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)

Here you can find the source of diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)

Description

returns the commercial day difference between two dates

License

Open Source License

Parameter

Parameter Description
dateUntil until (later) date
dateFrom from (earlier) date
bAddDay is same day a difference of one?

Return

day difference

Declaration

public static int diffCommercial(Date dateUntil, Date dateFrom,
        boolean bAddDay) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

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

public class Main {
    /**//  www  .  ja v  a 2 s.c o m
     * returns the commercial day difference between two dates
     * @param dateUntil until (later) date
     * @param dateFrom from (earlier) date
     * @param bAddDay is same day a difference of one?
     * @return day difference
     */
    public static int diffCommercial(Date dateUntil, Date dateFrom,
            boolean bAddDay) {
        int result = 0;
        if (dateFrom.after(dateUntil)) {
            result = -1;
        } else if (dateFrom.equals(dateUntil)) {
            result = (bAddDay ? 1 : 0);
        } else {
            final Calendar calFrom = new GregorianCalendar();
            calFrom.setTime(dateFrom);
            final Calendar calUntil = new GregorianCalendar();
            calUntil.setTime(dateUntil);
            final Calendar calTemp = new GregorianCalendar();
            while (!calFrom.after(calUntil)) {
                calTemp.setTime(calFrom.getTime());
                calTemp.set(GregorianCalendar.DAY_OF_MONTH, calFrom
                        .getActualMaximum(GregorianCalendar.DAY_OF_MONTH));
                calTemp.setTimeInMillis(Math.min(calTemp.getTimeInMillis(),
                        calUntil.getTimeInMillis()));
                if (calFrom.get(GregorianCalendar.DAY_OF_MONTH) == calFrom
                        .getActualMinimum(GregorianCalendar.DAY_OF_MONTH)
                        && calTemp.get(GregorianCalendar.DAY_OF_MONTH) == calTemp
                                .getActualMaximum(GregorianCalendar.DAY_OF_MONTH)) {
                    result += 30;
                } else {
                    result += calTemp.get(GregorianCalendar.DAY_OF_MONTH)
                            - calFrom.get(GregorianCalendar.DAY_OF_MONTH);
                }
                calFrom.add(GregorianCalendar.MONTH, 1);
                calFrom.set(GregorianCalendar.DAY_OF_MONTH, calFrom
                        .getActualMinimum(GregorianCalendar.DAY_OF_MONTH));
            }
            result += (bAddDay ? 1 : 0);
        }
        return result;
    }
}

Related

  1. diff(Date sDate, Date fDate)
  2. diff(Date subtrahend, Date minuend, long diffField)
  3. diff(int type, Date date1, Date date2)
  4. diff_in_date(Date d1, Date d2, String type)
  5. diffBetweenMonth(Date d1, Date d2)
  6. diffDate(Date date, Date date1)
  7. diffDate(java.util.Date date, java.util.Date date1)
  8. diffDateD(Date sd, Date ed)
  9. diffDateM(Date sd, Date ed)