java判断日期是否是节假日
java判断⽇期是否是节假⽇
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.MalformedURLException;
import java.URL;
ParseException;
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 = Excel();
try {
FileInputStream fin = new FileInputStream(excel);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(fin);
HSSFSheet sheet = SheetAt(0);
int last = LastRowNum();
int index = 1;
Date dt = null;
while (index <= last) {
HSSFRow row = Row(index);
/* 读取法定节假⽇ */
HSSFCell cell = Cell((short) 0);
if (cell != null) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
dt = JavaDate(cell
.getNumericCellValue());
if (dt != null && dt.getTime() > 0) {
this.festival.add(dt);
}
}
}
/* 读取特殊⼯作⽇ */
cell = Cell((short) 1);
if (cell != null) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
dt = JavaDate(cell
.getNumericCellValue());
if (dt != null && dt.getTime() > 0) {
// System.out.Date(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 = Resource("/");
url = new URL(url, "../" + FILE_NAME);
excel = new Path());
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 = Instance();
Calendar dcal = Instance();
dcal.setTime(date);
List<Date> list = Festival();
for (Date dt : list) {
fcal.setTime(dt);
// 法定节假⽇判断
if ((Calendar.MONTH) == (Calendar.MONTH)
&& (Calendar.DATE) == (Calendar.DATE)) {
festival = true;
}
}
return festival;
/**
* 周六周⽇判断
*
* @param date
* @return
*/
public boolean isWeekend(Date date) {
boolean weekend = false;
Calendar cal = Instance();
cal.setTime(date);
if ((Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| (Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
weekend = true;
}
return weekend;
}
/**
* 是否是⼯作⽇法定节假⽇和周末为⾮⼯作⽇
*
* @param date
* @return
*/
public boolean isWorkDay(Date date) {
boolean workday = true;
safe and sound 歌词if (this.isFestival(date) || this.isWeekend(date)) {
workday = false;
}
/* 特殊⼯作⽇判断 */
Calendar cal1 = Instance();
cal1.setTime(date);
春节高速免费通行时间2022Calendar cal2 = Instance();
for (Date dt : this.workDay) {
cal2.setTime(dt);
if ((Calendar.YEAR) == (Calendar.YEAR)
&& (Calendar.MONTH) == (Calendar.MONTH)
&& (Calendar.DATE) == (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));
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。