计算工作日,包括假期

我需要计算两个日期之间的工作日。 例如:我们7月4日有假期(在美国)。 所以,如果我的日期是date1 = 07/03/2012 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; } 

正如你所提到的,让我们假装你有一个包含所有假期的清单。

 ArrayList holidays = ... 

只需在do-whileif条件添加条件:

 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相同。

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 } ] 

我没有任何代码示例或类似的东西,但我做了一些搜索,并遇到了这个Stack Overflow线程,它有一些链接到Web服务,可以为你返回假日日期,这可能会帮助你到达你的位置需要: 国家假日网络服务

该主题中的最佳答案链接到此Web服务: http : //www.holidaywebservice.com/

我不确定使用网络服务是否过度杀戮,但肯定有更好的方法。 我道歉,我不是最有经验的程序员,所以我无法帮助你,就像我想的那样。