자바 두 날짜 사이 의 표준 근무일 (오리지널)

3846 단어
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);

    }  
}

좋은 웹페이지 즐겨찾기