java生成一年中假日表(包括週末和法定假期),用於計算一年中的工做日

思路:計算一個時間區間內工做日的天數,只要計算出這個時間區間的天數,再減去休息日的天數就能夠了。可是有點麻煩的是休息日的統計,由於它不只包括週末時間,還包括放假時間,放假還要考慮週末補班的,工做日放假休息的。 java

步驟: mysql

一、把一年內的全部週末加入到假期表中。 web

二、把一年中全部的法定假期加入到假期表中(需等國務院公佈,因此要一年跑一次維護假期表)。 sql

三、把週末須要補班的日子從表中剔除掉。 數據庫

上面還有一個細節就是法定假期裏可能也有周末,因此插入前要檢查避免重複插入。這樣咱們就獲得了一張包括一年中全部假期的假期數據表了。具體實現代碼以下,純屬能跑沒仔細優化: dom

package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
/**
 * 將一年內的全部假日插入到假日表裏
 * @author ch
 * @time 2016-1-15 下午6:06:11
 */
public class InsertHolidayUtil {
  public static void main(String[] args){
    //驅動程序名
     String driver = "com.mysql.jdbc.Driver"; 
     //要插入的數據庫,表
     String url = "jdbc:mysql://127.0.0.1:3306/xx_web";  
     String user = "root"; 
     String password = "123456";
     try {    
               //加載驅動程序
               Class.forName(driver);  
               //連續MySQL 數據庫
               Connection conn = DriverManager.getConnection(url, user, password);
               if(!conn.isClosed())
               System.out.println("Succeeded connecting to the Database!");
               //statement用來執行SQL語句
               Statement statement = conn.createStatement();
               
               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
               java.util.Date start = sdf.parse("2016-01-01");//開始時間
               java.util.Date end = sdf.parse("2016-12-31");//結束時間
               List<Date> lists = dateSplit(start, end);
               
               //-------------------插入週末時間---------------
               if (!lists.isEmpty()) {
                   for (Date date : lists) {
                	   Calendar cal = Calendar.getInstance();
                	    cal.setTime(date);
                       if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
                       {
                    	   System.out.println("插入日期:" + sdf.format(date) + ",週末");
                    	   String insertSql = "INSERT INTO fn_all_holiday (id,title,holiday_date) VALUES('"+UUID.randomUUID()+"',"+"'週末','"+sdf.format(date)+"')";
                    	   statement.executeUpdate(insertSql);
                       }
                   }
               }
               
               //---------------插入節假日時間------------------
               List<Days> holidays = new ArrayList<Days>();
               holidays.add(new Days(UUID.randomUUID().toString(),"元旦", sdf.parse("2016-01-01")));
               holidays.add(new Days(UUID.randomUUID().toString(),"元旦", sdf.parse("2016-01-02")));
               holidays.add(new Days(UUID.randomUUID().toString(),"元旦", sdf.parse("2016-01-03")));
               
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-07")));
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-08")));
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-09")));
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-10")));
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-11")));
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-12")));
               holidays.add(new Days(UUID.randomUUID().toString(),"春節", sdf.parse("2016-02-13")));
               
               holidays.add(new Days(UUID.randomUUID().toString(),"清明節", sdf.parse("2016-04-02")));
               holidays.add(new Days(UUID.randomUUID().toString(),"清明節", sdf.parse("2016-04-03")));
               holidays.add(new Days(UUID.randomUUID().toString(),"清明節", sdf.parse("2016-04-04")));
               
               holidays.add(new Days(UUID.randomUUID().toString(),"勞動節", sdf.parse("2016-04-30")));
               holidays.add(new Days(UUID.randomUUID().toString(),"勞動節", sdf.parse("2016-05-01")));
               holidays.add(new Days(UUID.randomUUID().toString(),"勞動節", sdf.parse("2016-05-02")));
               
               holidays.add(new Days(UUID.randomUUID().toString(),"端午節", sdf.parse("2016-06-09")));
               holidays.add(new Days(UUID.randomUUID().toString(),"端午節", sdf.parse("2016-06-10")));
               holidays.add(new Days(UUID.randomUUID().toString(),"端午節", sdf.parse("2016-06-11")));
               
               holidays.add(new Days(UUID.randomUUID().toString(),"中秋節", sdf.parse("2016-09-15")));
               holidays.add(new Days(UUID.randomUUID().toString(),"中秋節", sdf.parse("2016-09-16")));
               holidays.add(new Days(UUID.randomUUID().toString(),"中秋節", sdf.parse("2016-09-17")));
               
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-01")));
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-02")));
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-03")));
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-04")));
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-05")));
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-06")));
               holidays.add(new Days(UUID.randomUUID().toString(),"國慶節", sdf.parse("2016-10-07")));
               for(Days day:holidays) {
            	   //跟週末衝突的,不重複插入
                   String sql = "select count(1) as numbers from fn_all_holiday where holiday_date ='" + sdf.format(day.getDate()) + "'";
                   //結果集
                   ResultSet rs = statement.executeQuery(sql);
                   boolean hasRecord = false;
                   while(rs.next()) {
                	   if(!"0".equals(rs.getString("numbers"))) {
                		   hasRecord = true;
                	   }
                   }
                   if(!hasRecord) {
                	   System.out.println("插入日期:" + sdf.format(day.getDate()) + "," + day.getTitle());
                	   String insertSql = "INSERT INTO fn_all_holiday (id,title,holiday_date) VALUES('"+day.getId()+"',"+"'"+day.getTitle()+"','"+sdf.format(day.getDate())+"')";
                	   statement.executeUpdate(insertSql);
                   }
               }
               
               
               //-------------- 剔除補班時間(週末須要補班的)---------------------
               List<Days> workDays = new ArrayList<Days>();
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-02-06")));
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-02-14")));
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-05-02")));
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-06-12")));
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-09-18")));
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-10-08")));
               workDays.add(new Days(UUID.randomUUID().toString(),"補班", sdf.parse("2016-10-09")));
               
               for(Days day:workDays) {
            	   System.out.println("剔除日期:" + sdf.format(day.getDate()) + "," + day.getTitle());
            	   String delSql = "delete from fn_all_holiday where holiday_date ='" + sdf.format(day.getDate()) + "'";
            	   statement.executeUpdate(delSql);
               }
               conn.close();
         }
      catch(ClassNotFoundException e) { 
        System.out.println("Sorry,can't find the Driver!");
        e.printStackTrace();
       }
      catch(SQLException e) {
        e.printStackTrace();
      }
      catch(Exception e) {  
        e.printStackTrace(); 
       }
  }
  
  private static List<Date> dateSplit(java.util.Date start, Date end)
	        throws Exception {
	    if (!start.before(end))
	        throw new Exception("開始時間應該在結束時間以後");
	    Long spi = end.getTime() - start.getTime();
	    Long step = spi / (24 * 60 * 60 * 1000);// 相隔天數

	    List<Date> dateList = new ArrayList<Date>();
	    dateList.add(end);
	    for (int i = 1; i <= step; i++) {
	        dateList.add(new Date(dateList.get(i - 1).getTime()
	                - (24 * 60 * 60 * 1000)));// 比上一天減一
	    }
	    return dateList;
	}

}



package com.util;

import java.util.Date;

public class Days {
	private String id;
	private String title;
	private Date date;
	
	public Days(String id,String title,Date date) {
		this.id = id;
		this.title = title;
		this.date = date;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
	
}
相關文章
相關標籤/搜索