1.新建一个excel命名为“节假日.xls”存放节假日,模板格式如下

2.判断是否是节假日的类 工作日返回true ,休息日返回false。

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

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;

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;

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

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

try {

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);

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

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));
 }

}

java 判断日期是否是节假日的更多相关文章

  1. java判断日期与星期

    原文:http://www.open-open.com/code/view/1440592372888 import java.text.SimpleDateFormat; import java.u ...

  2. Java 判断日期的方法

    //str:传入的日期 eg:"2018-07-23" function IsDate(str) { arr = str.split("-"); if(arr. ...

  3. Java判断指定日期是否为工作日

    Java判断指定日期是否为工作日 转自:https://www.jianshu.com/p/966659492f2f 转:https://www.jianshu.com/p/05ccb5783f65转 ...

  4. JAVA判断当前日期是节假日还是工作日

    package com.buybal.bat.util; import java.io.File; import java.io.FileInputStream; import java.io.IOE ...

  5. Java中用正则表达式判断日期格式是否正确

    1.Java中用正则表达式判断日期格式是否正确 DateType.java: /** * @Title:DateType.java * @Package:com.you.dao * @Descript ...

  6. Java判断字符串是否符合yyyyMMdd日期格式

    Java判断字符串是否符合yyyyMMdd日期格式 代码: /** * 判断参数的格式是否为“yyyyMMdd”格式的合法日期字符串 * */ public static boolean isVali ...

  7. 判断日期是否为法定节假日的API接口与示例函数

    需要判定某日期是否为法定节假日,由于国家的节假日每年都在变动,所以使用接口判定是必要的.可惜的是,这样的接口并不多,在此汇总三个: http://tool.bitefu.net/jiari/ http ...

  8. Java Date 日期 时间 相关方法

    DateTools.java import java.text.SimpleDateFormat; import java.util.Date; /** * 日期操作类 */ public class ...

  9. Java 8 – 日期和时间实用技巧

    当你开始使用Java操作日期和时间的时候,会有一些棘手.你也许会通过System.currentTimeMillis() 来返回1970年1月1日到今天的毫秒数.或者使用Date类来操作日期:当遇到加 ...

随机推荐

  1. asp.net 将repeater上数据导出到excel

    1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...

  2. [转]Android Service完全解析,关于服务你所需知道的一切

      目录(?)[+] Android Service完全解析,关于服务你所需知道的一切(上) 分类: Android疑难解析2013-10-31 08:10 6451人阅读 评论(39) 收藏 举报 ...

  3. [转]Eclipse 的快捷键以及文档注释、多行注释的快捷键

    一.多行注释快捷键 1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释 2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释 3.以上快 ...

  4. Codeforces Beta Round #6 (Div. 2 Only) B. President's Office 水题

    B. President's Office 题目连接: http://codeforces.com/contest/6/problem/B Description President of Berla ...

  5. codeforces 19D D. Points 树套树

    D. Points Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/19/problem/D De ...

  6. 厄拉多塞筛法和普通方法求素数表(python实现)

    厄拉多赛筛法(sieve of Eratosthenes): 想要得到一个不大于N的数所有素数,可以先找到不超过根号N的所有素数,设2 = p1 < p2 < ......<pk ≤ ...

  7. [置顶] ubuntu server sudo出现sudo:must be setuid root 完美解决办法。

    1.开机按shift或esc先进行recovery模式 2.选择root命令行模式 3.先执行 #mount -o remount,rw / 这个很重要,网上找的很多资料都不全没有这步造成无法恢复成功 ...

  8. printf回到上一行开头以及回到本行开头的方法

    回到上一行开头 #include <stdio.h> #include <unistd.h> int main(void) { ; ){ printf("%d\n&q ...

  9. 《Haskell趣学指南》

    <Haskell趣学指南> 基本信息 原书名:Learn You a Haskell for Great Good!: A Beginner's Guide 原出版社: No Starch ...

  10. (原创)2. WPF中的依赖属性之二

    1 依赖属性 1.1 依赖属性最终值的选用 WPF属性系统对依赖属性操作的基本步骤如下: 第一,确定Base Value,对同一个属性的赋值可能发生在很多地方.还用Button的宽度来进行举例,可能在 ...