下面的代码计算要添加的工作日数,如果结束日期是假日/周末,则将日期转移到第二天。
但是这段代码假设开始日期不是在周末/假日。
如果开始日期属于周末/假日,我想要的代码也可以使用。
请注意此代码已发布 ElenaSofea于2013年6月17日,但由于我无法评论它,因此将此问作为一个新问题 参考:How to add a number of days to a Date while skipping weekends and other holidays
static DateTime CalculateFutureDate(DateTime fromDate, int numberofWorkDays,
ICollection<DateTime> holidays)
{
var futureDate = fromDate;
for (var i = 0; i < numberofWorkDays; i++ )
{
if (futureDate.DayOfWeek == DayOfWeek.Saturday
|| futureDate.DayOfWeek == DayOfWeek.Sunday
|| (holidays != null && holidays.Contains(futureDate)))
{
futureDate = futureDate.AddDays(1);
numberofWorkDays++;
}
else
{
futureDate = futureDate.AddDays(1);
}
}
while(futureDate.DayOfWeek == DayOfWeek.Saturday
|| futureDate.DayOfWeek == DayOfWeek.Sunday
|| (holidays != null && holidays.Contains(futureDate)))
{
futureDate = futureDate.AddDays(1);
}
return futureDate;
}
答案 0 :(得分:0)
尝试将日期作为输入传递,以便此方法验证以检查它是星期六还是星期日
public static boolean isValidateSundayorSaturday(String date) throws Exception {
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(getDate(date,null));
int day = calendar.get(Calendar.DAY_OF_WEEK);
if (day == Calendar.SUNDAY || day == Calendar.SATURDAY) {
return true;
}
}
return false;
}
答案 1 :(得分:0)
您必须提供所有排除日期,因为即使在同一个国家/地区也不同。
如果您想查看代码here you go