1. Create a new Excel named "Holiday. xls" to store holidays, template format as follows
2. Determine if the class working day of the holiday returns True, and the rest day returns false.
You need to refer to the poi-bin-3.9 package and put the package in the blog file
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 = "holiday. xls";
Private List<date> Festival = new arraylist<date> ();//Holidays
Private list<date> WorkDay = new arraylist<date> ();//Weekday
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);
/* Read Statutory holidays */
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);
}
}
}
/* Read Special Weekday */
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;
}
/**
* Read the holidays from the Excel file
*
* @return
*/
Public List Getfestival () {
return this.festival;
}
Public List Getspecialworkday () {
return this.workday;
}
/**
* Determine whether a date is a day of holiday holidays only judge the month and day, do not judge the year
*
* @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);
Statutory holiday judgment
if (Fcal.get (calendar.month) = = Dcal.get (calendar.month)
&& fcal.get (calendar.date) = = Dcal.get (calendar.date)) {
Festival = true;
}
}
Return festival;
}
/**
* Saturday Sunday Judgment
*
* @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;
}
/**
* is weekday holidays and weekends are nonworking days
*
* @param date
* @return
*/
&N Bsp;public boolean isworkday (date date) {
boolean workday = true;
if (this.isfestival (date) | | this.isweekend (DATE)) {
workday = false;
}
/* Special Business Day judgment */
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)) {//month-date equal to special working days
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 ();//Take time
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 to determine whether the date is a holiday