2014-10-31 75 views
0

Excel中有一個名爲NETWORKDAYS.INTL的函數。它會收到開始日期,結束日期,週末「類型」和假期列表,並計算兩個日期之間的工作天數。週末的國旗很重要,因爲我可以把星期六當作工作日。這裏更多的信息:NETWORKDAYS.INTL in Java

http://office.microsoft.com/en-nz/excel-help/networkdays-intl-function-HA010354379.aspx

有什麼樣NETWORKDAYS.INTL在Java中?使用ObjectLab Kit,Joda和Jollyday我可以保留我的國家假期,並檢查一天是星期六,星期日還是假期,但要像上面的函數那樣計算,我需要每天迭代並檢查是否有效。

回答

0

我什麼都沒發現,所以我只是編碼我自己的方法。在我的應用程序中,所有日期都保存爲時間戳。

public boolean isHoliday(long date) { 
    return holidaysCalendar.isHoliday(new LocalDate(date)); 
} 

public boolean isSaturday(long date) { 
    return (new LocalDate(date).dayOfWeek().get() == 7); 
} 

public boolean isSunday(long date) { 
    return (new LocalDate(date).dayOfWeek().get() == 1); 
} 

public boolean isValid(long date, boolean saturday, boolean sunday) { 
    boolean valid = true; 
    if (isHoliday(date)) { 
     valid = false; 
    } else { 
     if (!saturday && isSaturday(date)) { 
      valid = false; 
     } 
     if (!sunday && isSunday(date)) { 
      valid = false; 
     } 
    } 
    return valid; 
} 

public long addDays(long date, int days) { 
    return new LocalDate(date).plusDays(days).toDateTimeAtStartOfDay() 
      .getMillis(); 
} 

public int validDaysBetweenDates(long date1, long date2, 
     boolean saturday, boolean sunday) { 
    date1 = addDias(date1, 1); 
    int cont = 0; 
    for (long dateTmp = date1; dataTmp <= date2; dataTmp = addDays(dataTmp, 1)) { 
     if (isValid(dataTmp, saturday, sunday)) 
      cont++; 
    } 
    return cont; 
}