Here you can find the source of diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)
Parameter | Description |
---|---|
dateUntil | until (later) date |
dateFrom | from (earlier) date |
bAddDay | is same day a difference of one? |
public static int diffCommercial(Date dateUntil, Date dateFrom, boolean bAddDay)
//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; } }