2012-06-01 118 views
4

我需要計算兩個日期之間的工作日。 例如:我們在7月4日假期(在美國)。所以如果我的日期是 date1 = 2012年7月3日 date2 = 07/06/2012計算工作日包括假期

沒有工作日b/w這些日期應該是1,因爲7月4日是假期。

我有一個下面的方法來計算工作日,這將只計算週末而不是假期。 有沒有什麼方法可以計算假期....請幫助我。

public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) { 
    Calendar startCal; 
    Calendar endCal; 
    startCal = Calendar.getInstance(); 
    startCal.setTime(startDate); 
    endCal = Calendar.getInstance(); 
    endCal.setTime(endDate); 
    int workDays = 0; 

    //Return 0 if start and end are the same 
    if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) { 
     return 0; 
    } 

    if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) { 
     startCal.setTime(endDate); 
     endCal.setTime(startDate); 
    } 

    do { 
     startCal.add(Calendar.DAY_OF_MONTH, 1); 
     if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY 
     && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) { 
      ++workDays; 
     } 
    } while (startCal.getTimeInMillis() < endCal.getTimeInMillis()); 

    return workDays; 
} 
+0

你需要跟蹤手動假日的知識,AFAIK沒有內置的功能。另外,誰最近在java中使用do-while? –

+0

首先,您需要一年中的所有假期列表,因爲沒有java類(即'Locale'特定類)提供此功能。然後你需要找出這個列表中有多少個落在指定的日期之間,這是相當直接的。然後,您可以從上述代碼的結果中刪除那麼多天。 – WickeD

+0

感謝您的回覆。比方說,如果我有一個列表包含所有的假期,你可以建議我如何使用該列表或如何檢查條件中的列表日期。 – ran

回答

6

讓我們假裝你有一個包含所有假期的列表,正如你所提到的。

ArrayList<Integer> holidays = ... 

只需添加一個條件,你if條件的do-while

do { 
      startCal.add(Calendar.DAY_OF_MONTH, 1); 
      if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY 
      && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY 
      && !holidays.contains((Integer) startCal.get(Calendar.DAY_OF_YEAR))) { 
       ++workDays; 
      } 
} while (startCal.getTimeInMillis() < endCal.getTimeInMillis()); 

爲了簡便起見,我假定holiday包含格式相同Calendar.DAY_OF_YEAR日期。

+0

非常感謝你......我不知道我是如何錯過了這個簡單的邏輯。但在這裏而不是Calendar.DAY_OF_YEAR,我將它與Calendar.getTime()進行比較以返回完整日期。它現在工作正常 – ran

+0

乾杯,很高興成爲服務。 – SimplyPanda

+0

值得注意的是,這裏的兩個例子將計算1毫秒或更多的差異。改爲在循環中使用day_of_year和year比較。 – Eddie

1

我沒有任何代碼樣本或類似的東西,但我做了一些搜索,爲您和跨越這個堆棧溢出線程有一些鏈接到Web服務,可以返回的度假日期爲你,來到這可以幫助你得到你需要的是:National holiday web service

在該線程鏈接到本網站服務頂端回答:http://www.holidaywebservice.com/

我不知道,如果使用網絡服務的這種類型的事情是矯枉過正或不,但肯定有更好的方法。我很抱歉,我不是最有經驗的程序員,所以我不能盡我所能地幫助你。

+1

這不是真的矯枉過正。您需要從* somewhere *其他地方獲取信息,因爲它總是會發生變化。 – djechlin

1

Nager.Date

可以使用Nager.Date項目JSON API。它支持美國,加拿大和歐洲。每年可用的數據可以將信息保存在您自己的數據庫中。

//https://github.com/FasterXML/jackson-databind/ 
ObjectMapper mapper = new ObjectMapper(); 
MyValue value = mapper.readValue(new URL("http://date.nager.at/api/v1/get/US/2017"), PublicHoliday[].class); 

PublicHoliday.class

public class PublicHoliday 
{ 
    public String Date; 
    public String LocalName; 
    public String Name; 
    public String CountryCode; 
    public Boolean Fixed; 
    public Boolean CountyOfficialHoliday; 
    public Boolean CountyAdministrationHoliday; 
    public Boolean Global; 
    public String[] Counties; 
    public int LaunchYear; 
} 

檢索例JSON數據。

[ 
    { 
    "date": "2017-01-01", 
    "localName": "New Year's Day", 
    "name": "New Year's Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-01-16", 
    "localName": "Martin Luther King, Jr. Day", 
    "name": "Martin Luther King, Jr. Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-01-20", 
    "localName": "Inauguration Day", 
    "name": "Inauguration Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": false, 
    "counties": [ 
     "US-DC", 
     "US-LA", 
     "US-MD", 
     "US-VA" 
    ], 
    "launchYear": null 
    }, 
    { 
    "date": "2017-02-20", 
    "localName": "Washington's Birthday", 
    "name": "Presidents' Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-05-29", 
    "localName": "Memorial Day", 
    "name": "Memorial Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-07-04", 
    "localName": "Independence Day", 
    "name": "Independence Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-09-04", 
    "localName": "Labor Day", 
    "name": "Labor Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-09-09", 
    "localName": "Columbus Day", 
    "name": "Columbus Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": false, 
    "counties": [ 
     "US-AL", 
     "US-AZ", 
     "US-CO", 
     "US-CT", 
     "US-DC", 
     "US-GA", 
     "US-ID", 
     "US-IL", 
     "US-IN", 
     "US-IA", 
     "US-KS", 
     "US-KY", 
     "US-LA", 
     "US-ME", 
     "US-MD", 
     "US-MA", 
     "US-MS", 
     "US-MO", 
     "US-MT", 
     "US-NE", 
     "US-NH", 
     "US-NJ", 
     "US-NM", 
     "US-NY", 
     "US-NC", 
     "US-OH", 
     "US-OK", 
     "US-PA", 
     "US-RI", 
     "US-SC", 
     "US-TN", 
     "US-UT", 
     "US-VA", 
     "US-WV" 
    ], 
    "launchYear": null 
    }, 
    { 
    "date": "2017-11-10", 
    "localName": "Veterans Day", 
    "name": "Veterans Day", 
    "countryCode": "US", 
    "fixed": false, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    }, 
    { 
    "date": "2017-12-23", 
    "localName": "Thanksgiving Day", 
    "name": "Thanksgiving Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": 1863 
    }, 
    { 
    "date": "2017-12-25", 
    "localName": "Christmas Day", 
    "name": "Christmas Day", 
    "countryCode": "US", 
    "fixed": true, 
    "countyOfficialHoliday": true, 
    "countyAdministrationHoliday": true, 
    "global": true, 
    "counties": null, 
    "launchYear": null 
    } 
]