java获取工作日

  • Post author:
  • Post category:java


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Test {
         /**
	 * 获取某年某月内工作日
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private static List<Date> getDates(int year, int month) {
		List<Date> dates = new ArrayList<Date>();

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DATE, 1);
		while (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) < month) {
			int day = cal.get(Calendar.DAY_OF_WEEK);

			if (!(day == Calendar.SUNDAY || day == Calendar.SATURDAY)) {
				dates.add((Date) cal.getTime().clone());
			}
			cal.add(Calendar.DATE, 1);
		}
		return dates;

	}

	public static void main(String[] args) {
		List<Date> dates = getDates(2016, 2);
		for (Date date : dates) {
			System.out.println(date);
		}

	}
}



版权声明:本文为clypm原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。