java 判斷日期是不是節假日

1.新建一個excel命名爲「節假日.xls」存放節假日,模板格式以下java

2.判斷是不是節假日的類 工做日返回true ,休息日返回false。apache

 須要引用poi-bin-3.9包,包放在博客文件中this

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;url

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;.net

public class Festival {
 private final String FILE_NAME = "節假日.xls";
 private List<Date> festival = new ArrayList<Date>();// 節假日
 private List<Date> workDay = new ArrayList<Date>();// 工做日3d

 public Festival() {
  File excel = this.getExcel();excel

  try {orm

   FileInputStream fin = new FileInputStream(excel);
   HSSFWorkbook hssfworkbook = new HSSFWorkbook(fin);
   HSSFSheet sheet = hssfworkbook.getSheetAt(0);
   int last = sheet.getLastRowNum();
   int index = 1;
   Date dt = null;
   while (index <= last) {
    HSSFRow row = sheet.getRow(index);blog

    /* 讀取法定節假日 */
    HSSFCell cell = row.getCell((short) 0);
    if (cell != null) {
     if (HSSFDateUtil.isCellDateFormatted(cell)) {
      dt = HSSFDateUtil.getJavaDate(cell
        .getNumericCellValue());ci

      if (dt != null && dt.getTime() > 0) {
       this.festival.add(dt);

      }
     }

    }

    /* 讀取特殊工做日 */
    cell = row.getCell((short) 1);

    if (cell != null) {
     if (HSSFDateUtil.isCellDateFormatted(cell)) {
      dt = HSSFDateUtil.getJavaDate(cell
        .getNumericCellValue());

      if (dt != null && dt.getTime() > 0) {
       // System.out.println(this.getDate(dt));
       this.workDay.add(dt);
      }
     }

    }

    index++;
   }
   fin.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public File getExcel() {
  File excel = null;
  try {
   URL url = Festival.class.getResource("/");
   url = new URL(url, "../" + FILE_NAME);
   excel = new File(url.getPath());
   return excel;
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return excel;
 }

 /**
  * 從EXCEL文件中讀取節假日
  *
  * @return
  */
 public List getFestival() {
  return this.festival;
 }

 public List getSpecialWorkDay() {
  return this.workDay;
 }

 /**
  * 判斷一個日期是否日節假日 法定節假日只判斷月份和天,不判斷年
  *
  * @param date
  * @return
  */
 public boolean isFestival(Date date) {
  boolean festival = false;
  Calendar fcal = Calendar.getInstance();
  Calendar dcal = Calendar.getInstance();
  dcal.setTime(date);
  List<Date> list = this.getFestival();
  for (Date dt : list) {
   fcal.setTime(dt);

   // 法定節假日判斷
   if (fcal.get(Calendar.MONTH) == dcal.get(Calendar.MONTH)
     && fcal.get(Calendar.DATE) == dcal.get(Calendar.DATE)) {
    festival = true;
   }
  }
  return festival;
 }

 /**
  * 週六週日判斷
  *
  * @param date
  * @return
  */
 public boolean isWeekend(Date date) {
  boolean weekend = false;
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
    || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
   weekend = true;
  }
  return weekend;
 }

 /**
  * 是不是工做日 法定節假日和週末爲非工做日
  *
  * @param date
  * @return
  */
 public boolean isWorkDay(Date date) {
  boolean workday = true;
  if (this.isFestival(date) || this.isWeekend(date)) {
   workday = false;
  }

  /* 特殊工做日判斷 */
  Calendar cal1 = Calendar.getInstance();
  cal1.setTime(date);
  Calendar cal2 = Calendar.getInstance();
  for (Date dt : this.workDay) {
   cal2.setTime(dt);
   if (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
     && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)
     && cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE)) { // 年月日相等爲特殊工做日
    workday = true;
   }
  }
  return workday;
 }

 public Date getDate(String str) {
  Date dt = null;
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  try {
   dt = df.parse(str);
  } catch (ParseException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return dt;

 }

 public String getDate(Date date) {
  String dt = null;
  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  dt = df.format(date);

  return dt;

 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Date date=new Date();//取時間

   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
   String dateString = formatter.format(date);
  
   System.out.println(dateString);

  Festival f = new Festival();
  Date dt = f.getDate(dateString);
  System.out.println(f.isWorkDay(dt));
 }

}

相關文章
相關標籤/搜索