java 两个日期之间的标准工作日(原创)

package control;


import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Map;

import javax.servlet.jsp.jstl.sql.Result;

public class DateOper {
	private String holidays;
	private String workdays;
	private DB_OPER db=new DB_OPER();
	public DateOper()
	{
		Result result = null;
		String sqlholiday = "select holidaydate from hrmpubholiday t where changetype=1";// 这个表手动维护,由国务院发布放假通知后添加
		String sqlworkday = "select holidaydate from hrmpubholiday t where changetype=3";

		result = db.executeQuery(sqlholiday);
		// 得到今年和明年的所有的假日


   	 if(result!=null && result.getRowCount()!=0)
   	 {
   		 for(int i=0;i<result.getRowCount();i++)
   		 {
   			 Map row = result.getRows()[i]; 
   	      	
   			holidays += row.get("holidaydate").toString() + ",";
   
   		 }
   	 }
   	  System.out.println(holidays);

	// 得到今年和明年的所有的假日

   	result = db.executeQuery(sqlworkday);
	 if(result!=null && result.getRowCount()!=0)
	 {
		 for(int i=0;i<result.getRowCount();i++)
		 {
			 Map row = result.getRows()[i]; 
	      	
			 workdays += row.get("holidaydate").toString() + ",";
	
		 }
	 }
	  System.out.println(workdays);
	}
	public  int countWorkDay(String start1, String end1) 
	{
		
		 
		 String strsql;
		// 工作日
		int workDay = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date start=null;
		Date end = null;

		try {
			start=(Date) sdf.parse(start1);
			end = (Date) sdf.parse(end1);
		} catch (ParseException e) {
		System.out.println("非法的日期格式,无法进行转换");
		e.printStackTrace();
		}
		//int thisyear = start.getYear() + 1900;
		//int nextyear = thisyear == end.getYear() ? thisyear + 1 : end.getYear() + 1900;

		System.out.println("start>>"+start+"   end>>"+end);
		int days = getDutyDays(start, end);
		System.out.println("start>>"+start1+"   end>>"+end1+"   days>>"+days);
		return days;
	}
 


	public  int getDutyDays(Date StartDate,Date EndDate) {//得到非周六周日的工作日
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		StartDate.setDate(StartDate.getDate() + 1);
		int result = 0;
		while (StartDate.compareTo(EndDate) <= 0) {
		
			if (StartDate.getDay() != 6 && StartDate.getDay() != 0)//周内
			{
				    result++;
				 // System.out.println(holidays);
				  if(!holidays.isEmpty())
				{
					  if(holidays.indexOf(sdf.format(StartDate))>-1)//不是节假日加1
					result--;
				}
				
			}
			else	//周末
			{    if(!workdays.isEmpty())
			     {
				if(workdays.indexOf(sdf.format(StartDate))>-1)//工作日
				 result++;
			     }
			}
		//	System.out.println(StartDate+"-------"+StartDate.getDay()+"-----"+result+"******"+holidays.indexOf(sdf.format(StartDate))+"******"+workdays.indexOf(sdf.format(StartDate)));
		StartDate.setDate(StartDate.getDate() + 1);
		}
		return result;
		}
	
	public static void main(String[] args) throws Exception {

		 
		DateOper db=new DateOper();
		
		int workDays=db.countWorkDay("2016-2-10","2016-2-26");
	    System.out.println("workdays="+workDays);

    }  
}

  

原文地址:https://www.cnblogs.com/zzlp/p/5166605.html