第3个回答 推荐于2016-10-30
package com.brilliance.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
/**
* <br> * Title: * <br>
* <br> * Description: * <br>
* <br> * Copyright: 2009.10 * <br>
* <br> * Company: Brilliance * <br>
*
* @author yunfeiyang3210
* @version v1.0
*
*/
public class NextWorkDay {
public static Date getNextWorkDay(Date curDate, int num, List<Date> holidays) {
// 为节假日进行排序
Collections.sort(holidays, new Comparator<Date>() {
public int compare(Date arg0, Date arg1) {
if (arg0.getTime() > arg1.getTime())
return 1;
else if (arg0.getTime() < arg1.getTime())
return -1;
else
return 0;
}
});
Date beginDate = curDate;
Date endDate = curDate;
for (int i = 0; i < num; i++) {
endDate = new Date(endDate.getTime() + 24 * 60 * 60 * 1000);
int day_of_week = NextWorkDay.getDayOfWeek(endDate);
if (day_of_week == 1) {
beginDate = endDate;
endDate = new Date(endDate.getTime() + 24 * 60 * 60 * 1000);
} else if (day_of_week == 7) {
beginDate = endDate;
endDate = new Date(endDate.getTime() + 2 * 24 * 60 * 60 * 1000);
}
for (Date holiday : holidays) {
if (holiday.after(beginDate) && holiday.compareTo(endDate) == 0) {
beginDate = holiday;
endDate = new Date(endDate.getTime() + 24 * 60 * 60 * 1000);
}
}
}
return endDate;
}
public static Integer getDayOfWeek(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
}
public static Date getDateFromString(String dateStr) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String[] dateStr = { "2009-09-25", "2009-10-01", "2009-10-02",
"2009-10-03", "2009-10-04", "2009-10-05", "2009-10-06" };
List<Date> holidays = new ArrayList<Date>();
for (int i = 0; i < 7; i++) {
Date holiday = NextWorkDay.getDateFromString(dateStr[i]);
holidays.add(holiday);
}
Date nextWorkDay = NextWorkDay.getNextWorkDay(NextWorkDay
.getDateFromString("2009-09-30"), 3, holidays);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(nextWorkDay));
}
}本回答被提问者采纳